/*************************************************************************************
*Copyright (c) 2011, Intel Mobile Communications GmbH. 
*All rights reserved.
*
*Redistribution and use in source and binary forms, with or without
*modification, are permitted provided that the following conditions are met:
*    * Redistributions of source code must retain the above copyright
*      notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright
*      notice, this list of conditions and the following disclaimer in the
*      documentation and/or other materials provided with the distribution.
*    * Neither the name of the <organization> nor the
*      names of its contributors may be used to endorse or promote products
*      derived from this software without specific prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
*ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
*WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*DISCLAIMED. IN NO EVENT SHALL INTEL MOBILE COMMUNICATIONS GMBH. BE LIABLE FOR ANY
*DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
*(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************************/

/*
This file contains samples of  the code for the article,
"Processing XML with Xerces and the DOM" by Ethan McCallum (2005/09/08)
Published on ONLamp.com (http://www.onlamp.com/)
http://www.onlamp.com/pub/a/onlamp/2005/09/08/xerces_dom.html

*/

#include "cef_parser.h"
using namespace std;
using namespace xercesc;
// = = = = = = = = = = = = = = = = = = = =


StringManager::StringManager()
	:
	xmlStrings_() ,
	cStrings_()
{

	return ;

} // StringManager::ctor


StringManager::~StringManager() throw(){

	drain() ;
	return ;

} // StringManager::dtor


void StringManager::drain() throw() {

	std::for_each(
		xmlStrings_.begin() ,
		xmlStrings_.end() ,
		FreeXercesString::releaseXMLString
	) ;


	std::for_each(
		cStrings_.begin() ,
		cStrings_.end() ,
		FreeXercesString::releaseCString
	) ;

	return ;

} // StringManager::drain()


char* StringManager::disown( char* str ) throw( std::logic_error ){

	CStringList::iterator item = std::find( cStrings_.begin() , cStrings_.end() , str ) ;

	if( cStrings_.end() == item ){
		throw( std::logic_error( "Request to disown a string not owned by this container." ) ) ;
	}

	char* result = *item ;

	// the call to list<>::remove has the added benefit of nailing duplicates
	cStrings_.remove( str ) ;

	return( result ) ;

} // StringManager::disown( char* str )

XMLCh* StringManager::disown( XMLCh* str ) throw( std::logic_error ){

	XMLStringList::iterator item = std::find( xmlStrings_.begin() , xmlStrings_.end() , str ) ;

	if( xmlStrings_.end() == item ){
		throw( std::logic_error( "Request to disown a string not owned by this container." ) ) ;
	}

	XMLCh* result = *item ;

	// the call to list<>::remove has the added benefit of nailing duplicates
	xmlStrings_.remove( str ) ;

	return( result ) ;

} // StringManager::disown( XMLCh* str )



XMLCh* StringManager::convert( const char* str ){

	XMLCh* result = xercesc::XMLString::transcode( str ) ;

	xmlStrings_.push_back( result ) ;

	return( result ) ;

} // StringManager::convert( const char* )

XMLCh* StringManager::convert( const std::string& str ){

	XMLCh* result = xercesc::XMLString::transcode( str.c_str() ) ;

	xmlStrings_.push_back( result ) ;

	return( result ) ;

} // StringManager::convert( const std::string& )

char* StringManager::convert( const XMLCh* str ){

	char* result = xercesc::XMLString::transcode( str ) ;

	cStrings_.push_back( result ) ;

	return( result ) ;

} // StringManager::convert( const XMLCh* )

cef_tags::cef_tags(): communication( XMLString::transcode( "communication" )),
	 cef( XMLString::transcode( "cef" )),
	 use_case( XMLString::transcode( "use_case" )),
	 system( XMLString::transcode( "system" )),
	 domains( XMLString::transcode( "domains" )),
	 domain( XMLString::transcode( "domain" )),
	 system_properties( XMLString::transcode( "system_properties" )),
	 blocks( XMLString::transcode( "blocks" )),
	 block( XMLString::transcode( "block" )),
	 links( XMLString::transcode( "links" )),
	 link( XMLString::transcode( "link" )),
	 routes( XMLString::transcode( "routes" )),
	 route( XMLString::transcode( "route" )),
	 fp_bounding_box( XMLString::transcode( "fp_bounding_box" )),
	 x( XMLString::transcode( "x" )),
	 y( XMLString::transcode( "y" )),
	 clocks( XMLString::transcode( "clocks" )),
	 clock( XMLString::transcode( "clock" )),
	 name( XMLString::transcode( "name" )),
	 id( XMLString::transcode( "id" )),
	 frequency( XMLString::transcode( "frequency" )),
	 skew( XMLString::transcode( "skew" )),
	 derived_from_id( XMLString::transcode( "derived_from_id" )),
	 derivation_type( XMLString::transcode( "derivation_type" )),
	 derivation_phase( XMLString::transcode( "derivation_phase" )),
	 power_networks( XMLString::transcode( "power_networks" )),
	 power_network( XMLString::transcode( "power_network" )),
	 power_network_id( XMLString::transcode( "power_network_id" )),
	 voltage( XMLString::transcode( "voltage" )),
	 clock_id( XMLString::transcode( "clock_id" )),
	 operating_points( XMLString::transcode( "operating_points" )),
	 operating_point( XMLString::transcode( "operating_point" )),
	 shutdownallowed( XMLString::transcode( "shutdownallowed" )),
	 type( XMLString::transcode( "type" )),
	 connections( XMLString::transcode( "connections" )),
	 share_edge_with( XMLString::transcode( "share_edge_with" )),
	 ports( XMLString::transcode( "ports" )),
	 port( XMLString::transcode( "port" )),
	 protocol( XMLString::transcode( "protocol" )),
	 protocol_variant( XMLString::transcode( "protocol_variant" )),
	 max_outstanding_transactions( XMLString::transcode( "max_outstanding_transactions" )),
	 address_width( XMLString::transcode( "address_width" )),
	 read_data_width( XMLString::transcode( "read_data_width" )),
	 write_data_width( XMLString::transcode( "write_data_width" )),
	 orientation( XMLString::transcode( "orientation" )),
	 size( XMLString::transcode( "size" )),
	 soft( XMLString::transcode( "soft" )),
	 shape( XMLString::transcode( "shape" )),
	 corners( XMLString::transcode( "corners" )),
	 corner( XMLString::transcode( "corner" )),
	 area( XMLString::transcode( "area" )),
	 aspectratio( XMLString::transcode( "aspectratio" )),
	 bottom_left_corner_position( XMLString::transcode( "bottom_left_corner_position" )),
	 movable( XMLString::transcode( "movable" )),
	 initial_x( XMLString::transcode( "initial_x" )),
	 initial_y( XMLString::transcode( "initial_y" )),
	 rotatable( XMLString::transcode( "rotatable" )),
	 mirrorable( XMLString::transcode( "mirrorable" )),
	 rotation( XMLString::transcode( "rotation" )),
	 initial_rotation( XMLString::transcode( "initial_rotation" )),
	 mirroring_x( XMLString::transcode( "mirroring_x" )),
	 initial_mirroring_x( XMLString::transcode( "initial_mirroring_x" )),
	 mirroring_y( XMLString::transcode( "mirroring_y" )),
	 initial_mirroring_y( XMLString::transcode( "initial_mirroring_y" )),
	 layer( XMLString::transcode( "layer" )),
	 domain_id( XMLString::transcode( "domain_id" )),
	 architecture( XMLString::transcode( "architecture" )),
	 clock_divider( XMLString::transcode( "clock_divider" )),
	 buffers( XMLString::transcode( "buffers" )),
	 in( XMLString::transcode( "in" )),
	 out( XMLString::transcode( "out" )),
	 address_ranges( XMLString::transcode( "address_ranges" )),
	 address_range( XMLString::transcode( "address_range" )),
	 start( XMLString::transcode( "start" )),
	 inports( XMLString::transcode( "inports" )),
	 outports( XMLString::transcode( "outports" )),
	 input_buffers( XMLString::transcode( "input_buffers" )),
	 depth( XMLString::transcode( "depth" )),
	 output_buffers( XMLString::transcode( "output_buffers" )),
	 flit_width( XMLString::transcode( "flit_width" )),
	 virtual_channels( XMLString::transcode( "virtual_channels" )),
	 virtual_channel_arbitration_structure( XMLString::transcode( "virtual_channel_arbitration_structure" )),
	 virtual_channel_arbitration_policy( XMLString::transcode( "virtual_channel_arbitration_policy" )),
	 source( XMLString::transcode( "source" )),
	 destination( XMLString::transcode( "destination" )),
	 number( XMLString::transcode( "number" )),
	 width( XMLString::transcode( "width" )),
	 carries_source_clock( XMLString::transcode( "carries_source_clock" )),
	 carries_source_reset( XMLString::transcode( "carries_source_reset" )),
	 link_length_estimation( XMLString::transcode( "link_length_estimation" )),
	 out_port_number( XMLString::transcode( "out_port_number" )),
	 in_port_number( XMLString::transcode( "in_port_number" )),
	 blocks_on_link( XMLString::transcode( "blocks_on_link" )),
	 block_on_link( XMLString::transcode( "block_on_link" )),
	 routingpolicy( XMLString::transcode( "routingpolicy" )),
	 source_routing( XMLString::transcode( "source_routing" )),
	 switches( XMLString::transcode( "switches" )),
	 switch_( XMLString::transcode( "switch" )),
	 outport( XMLString::transcode( "outport" )),
	 linknum( XMLString::transcode( "linknum" )),
	 route_weight( XMLString::transcode( "route_weight" )),
	 distributed_routing( XMLString::transcode( "distributed_routing" )),
	 tables( XMLString::transcode( "tables" )),
	 table( XMLString::transcode( "table" )),
	 switch_id( XMLString::transcode( "switch_id" )),
	 destination_id( XMLString::transcode( "destination_id" )),
	 input_port( XMLString::transcode( "input_port" )),
	 output_ports( XMLString::transcode( "output_ports" )),
	 output_port( XMLString::transcode( "output_port" )),
	 lbdr( XMLString::transcode( "lbdr" )),
	 lbdr_entry( XMLString::transcode( "lbdr_entry" )),
	 lbdr_routing_bits( XMLString::transcode( "lbdr_routing_bits" )),
	 lbdr_connectivity_bits( XMLString::transcode( "lbdr_connectivity_bits" )),
	 lbdr_deroute_bits( XMLString::transcode( "lbdr_deroute_bits" )),
	 lbdr_fork_bits( XMLString::transcode( "lbdr_fork_bits" )),
	 fdor( XMLString::transcode( "fdor" )),
	 fdor_entry( XMLString::transcode( "fdor_entry" )),
	 fdor_bit( XMLString::transcode( "fdor_bit" )),
	 virtual_networks( XMLString::transcode( "virtual_networks" )),
	 virtual_network( XMLString::transcode( "virtual_network" )),
	 vc_id( XMLString::transcode( "vc_id" )),
	 usecase( XMLString::transcode( "usecase" )),
	 dvfs( XMLString::transcode( "dvfs" )),
	 dvfs_point( XMLString::transcode( "dvfs_point" )),
	 initiators( XMLString::transcode( "initiators" )),
	 initiator( XMLString::transcode( "initiator" )),
	 initiator_port_id( XMLString::transcode( "initiator_port_id" )),
	 targets( XMLString::transcode( "targets" )),
	 target( XMLString::transcode( "target" )),
	 target_port_id( XMLString::transcode( "target_port_id" )),
	 flows( XMLString::transcode( "flows" )),
	 flow( XMLString::transcode( "flow" )),
	 flow_type( XMLString::transcode( "flow_type" )),
	 peak_bandwidth( XMLString::transcode( "peak_bandwidth" )),
	 sustained_bandwidth( XMLString::transcode( "sustained_bandwidth" )),
	 burst_transfers( XMLString::transcode( "burst_transfers" )),
	 burst_transfer( XMLString::transcode( "burst_transfer" )),
	 transfer_size( XMLString::transcode( "transfer_size" )),
	 period( XMLString::transcode( "period" )),
	 transfer_offset( XMLString::transcode( "transfer_offset" )),
	 transfer_duration( XMLString::transcode( "transfer_duration" )),
	 lat_zl( XMLString::transcode( "lat_zl" )),
	 lat_rt( XMLString::transcode( "lat_rt" )),
	 gid( XMLString::transcode( "gid" )),
	 burstiness( XMLString::transcode( "burstiness" )),
	 burst( XMLString::transcode( "burst" )),
	 length( XMLString::transcode( "length" )),
	 percent( XMLString::transcode( "percent" )),
	 vendor( XMLString::transcode( "vendor" )),
	 vendor_id( XMLString::transcode( "vendor_id" )),
	 vendor_name( XMLString::transcode( "vendor_name" )),
	 fp_width( XMLString::transcode( "fp_width" )),
	 fp_height( XMLString::transcode( "fp_height" )),
	 cef_version( XMLString::transcode ( "cef_version")),
	 block_type( XMLString::transcode ( "block_type")),
	 connection_with_id( XMLString::transcode ( "connection_with_id")),
	 position_x( XMLString::transcode ( "position_x")),
	position_y( XMLString::transcode ( "position_y")),
	ni_type( XMLString::transcode ( "ni_type")),
	arbitration_policy( XMLString::transcode ( "arbitration_policy")),
	source_port_id( XMLString::transcode ( "source_port_id")),
	destination_port_id( XMLString::transcode ( "destination_port_id")),
	auxilary_backward_wires( XMLString::transcode ( "auxilary_backward_wires")),
	auxilary_forward_wires( XMLString::transcode ( "auxilary_forward_wires")),
	source_id( XMLString::transcode ( "source_id")),
	switch_output_port_ids( XMLString::transcode ( "switch_output_port_ids")),
	switch_output_port_id( XMLString::transcode ( "switch_output_port_id")),
	output_port_id( XMLString::transcode ( "output_port_id")),
	lbdr_deroutes( XMLString::transcode ( "lbdr_deroutes")),
	lbdr_deroute( XMLString::transcode ( "lbdr_deroute")),
	input_port_id( XMLString::transcode ( "input_port_id")),
	deroute_port( XMLString::transcode ( "deroute_port")),
	lbdr_forks( XMLString::transcode ( "lbdr_forks")),
	lbdr_fork( XMLString::transcode ( "lbdr_fork")),
	fork_ports( XMLString::transcode ( "fork_ports")),
	fork_port( XMLString::transcode ( "fork_port")),
	fdor_bits( XMLString::transcode ( "fdor_bits")),
	vc_ids( XMLString::transcode ( "vc_ids")),
	domain_operating_points( XMLString::transcode ( "domain_operating_points")),
	operating_point_id( XMLString::transcode ( "operating_point_id")),
	weight( XMLString::transcode ( "weight")),
	zero_load_latency_bound( XMLString::transcode ( "zero_load_latency_bound")),
	latency_bound( XMLString::transcode ( "latency_bound")),
	priority_class( XMLString::transcode ( "priority_class"))
{
};

cef_tags::~cef_tags()
{
	XMLString::release(&communication);
	XMLString::release(&cef);
	XMLString::release(&use_case);
	XMLString::release(&system);
	XMLString::release(&domains);
	XMLString::release(&domain);
	XMLString::release(&system_properties);
	XMLString::release(&blocks);
	XMLString::release(&block);
	XMLString::release(&links);
	XMLString::release(&link);
	XMLString::release(&routes);
	XMLString::release(&route);
	XMLString::release(&fp_bounding_box);
	XMLString::release(&x);
	XMLString::release(&y);
	XMLString::release(&clocks);
	XMLString::release(&clock);
	XMLString::release(&name);
	XMLString::release(&id);
	XMLString::release(&frequency);
	XMLString::release(&skew);
	XMLString::release(&derived_from_id);
	XMLString::release(&derivation_type);
	XMLString::release(&derivation_phase);
	XMLString::release(&power_networks);
	XMLString::release(&power_network);
	XMLString::release(&power_network_id);
	XMLString::release(&voltage);
	XMLString::release(&clock_id);
	XMLString::release(&operating_points);
	XMLString::release(&operating_point);
	XMLString::release(&shutdownallowed);
	XMLString::release(&type);
	XMLString::release(&connections);
	XMLString::release(&share_edge_with);
	XMLString::release(&ports);
	XMLString::release(&port);
	XMLString::release(&protocol);
	XMLString::release(&protocol_variant);
	XMLString::release(&max_outstanding_transactions);
	XMLString::release(&address_width);
	XMLString::release(&read_data_width);
	XMLString::release(&write_data_width);
	XMLString::release(&orientation);
	XMLString::release(&soft);
	XMLString::release(&shape);
	XMLString::release(&corners);
	XMLString::release(&corner);
	XMLString::release(&area);
	XMLString::release(&aspectratio);
	XMLString::release(&bottom_left_corner_position);
	XMLString::release(&movable);
	XMLString::release(&initial_x);
	XMLString::release(&initial_y);
	XMLString::release(&rotatable);
	XMLString::release(&mirrorable);
	XMLString::release(&rotation);
	XMLString::release(&initial_rotation);
	XMLString::release(&mirroring_x);
	XMLString::release(&initial_mirroring_x);
	XMLString::release(&mirroring_y);
	XMLString::release(&initial_mirroring_y);
	XMLString::release(&layer);
	XMLString::release(&domain_id);
	XMLString::release(&architecture);
	XMLString::release(&clock_divider);
	XMLString::release(&buffers);
	XMLString::release(&in);
	XMLString::release(&out);
	XMLString::release(&address_ranges);
	XMLString::release(&address_range);
	XMLString::release(&start);
	XMLString::release(&size);
	XMLString::release(&inports);
	XMLString::release(&outports);
	XMLString::release(&input_buffers);
	XMLString::release(&depth);
	XMLString::release(&output_buffers);
	XMLString::release(&flit_width);
	XMLString::release(&virtual_channels);
	XMLString::release(&virtual_channel_arbitration_policy);
	XMLString::release(&source);
	XMLString::release(&destination);
	XMLString::release(&number);
	XMLString::release(&width);
	XMLString::release(&carries_source_clock);
	XMLString::release(&carries_source_reset);
	XMLString::release(&link_length_estimation);
	XMLString::release(&out_port_number);
	XMLString::release(&in_port_number);
	XMLString::release(&blocks_on_link);
	XMLString::release(&block_on_link);
	XMLString::release(&routingpolicy);
	XMLString::release(&source_routing);
	XMLString::release(&switches);
	XMLString::release(&switch_);
	XMLString::release(&outport);
	XMLString::release(&linknum);
	XMLString::release(&route_weight);
	XMLString::release(&distributed_routing);
	XMLString::release(&tables);
	XMLString::release(&table);
	XMLString::release(&switch_id);
	XMLString::release(&destination_id);
	XMLString::release(&input_port);
	XMLString::release(&output_ports);
	XMLString::release(&output_port);
	XMLString::release(&lbdr);
	XMLString::release(&lbdr_entry);
	XMLString::release(&lbdr_routing_bits);
	XMLString::release(&lbdr_connectivity_bits);
	XMLString::release(&lbdr_deroute_bits);
	XMLString::release(&lbdr_fork_bits);
	XMLString::release(&fdor);
	XMLString::release(&fdor_entry);
	XMLString::release(&fdor_bit);
	XMLString::release(&virtual_networks);
	XMLString::release(&virtual_network);
	XMLString::release(&vc_id);
	XMLString::release(&usecase);
	XMLString::release(&dvfs);
	XMLString::release(&dvfs_point);
	XMLString::release(&initiators);
	XMLString::release(&initiator);
	XMLString::release(&initiator_port_id);
	XMLString::release(&targets);
	XMLString::release(&target);
	XMLString::release(&target_port_id);
	XMLString::release(&flows);
	XMLString::release(&flow);
	XMLString::release(&flow_type);
	XMLString::release(&peak_bandwidth);
	XMLString::release(&sustained_bandwidth);
	XMLString::release(&burst_transfers);
	XMLString::release(&burst_transfer);
	XMLString::release(&transfer_size);
	XMLString::release(&period);
	XMLString::release(&transfer_offset);
	XMLString::release(&transfer_duration);
	XMLString::release(&lat_zl);
	XMLString::release(&lat_rt);
	XMLString::release(&gid);
	XMLString::release(&burstiness);
	XMLString::release(&burst);
	XMLString::release(&length);
	XMLString::release(&percent);
	XMLString::release(&vendor);
	XMLString::release(&vendor_id);
	XMLString::release(&vendor_name);
	XMLString::release(&fp_width);
	XMLString::release(&fp_height);
	XMLString::release(&cef_version);
	XMLString::release(&block_type);
	XMLString::release(&connection_with_id);
	XMLString::release(&position_x);
	XMLString::release(&position_y);
	XMLString::release(&ni_type);
	XMLString::release(&arbitration_policy);
	XMLString::release(&virtual_channel_arbitration_structure);
	XMLString::release(&source_port_id);
	XMLString::release(&destination_port_id);
	XMLString::release(&auxilary_backward_wires);
	XMLString::release(&auxilary_forward_wires);
	XMLString::release(&source_id);
	XMLString::release(&switch_output_port_ids);
	XMLString::release(&switch_output_port_id);
	XMLString::release(&output_port_id);
	XMLString::release(&lbdr_deroutes);
	XMLString::release(&lbdr_deroute);
	XMLString::release(&input_port_id);
	XMLString::release(&deroute_port);
	XMLString::release(&lbdr_forks);
	XMLString::release(&lbdr_fork);
	XMLString::release(&fork_ports);
	XMLString::release(&fork_port);
	XMLString::release(&fdor_bits);
	XMLString::release(&vc_ids);
	XMLString::release(&domain_operating_points);
	XMLString::release(&operating_point_id);
	XMLString::release(&weight);
	XMLString::release(&zero_load_latency_bound);
	XMLString::release(&latency_bound);
	XMLString::release(&priority_class);
}
	

bool cef_parser::toBool(string b)
{
	if(b=="1" || b=="true" || b=="True") return true;
	return false;
}

unsigned long long cef_parser::str2u(string str)
{
	string temp;
	unsigned long long out=0;
	for (unsigned int i = 0; i < str.length(); i++)
		if (str[i] != ' ') temp += str[i];
	for(unsigned i=0;i<temp.length();i++)
	{
			out=(out<<1) | (temp[i]=='1');
	}
	return out;
}

string cef_parser::u2str(unsigned long long n)
{
	unsigned long long p=1<<63;
	string out;
	const char* a="1";
	const char* b="1";
	const char* c=" ";
	for(unsigned i=0;i<64;i++)
	{	
		if(p & n)
		{
			out.append(a);					
		}	
		else
		{
			out.append(b);
		}
		if(i!=63) out.append(c);
	}
	return out;
}

void cef_parser::handleBlocks(DOMElement* element)
{
	DOMNodeList* children = element->getElementsByTagName(tags.block);
	blocks=new vector<cef_block*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		blocks->push_back( handleBlock((DOMElement*) children->item(i)));	
	}
	
}
void cef_parser::handleDomains(DOMElement* element)
{
	DOMNodeList* children = element->getElementsByTagName(tags.domain);
	domains=new vector<cef_domain*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		domains->push_back(handleDomain( (DOMElement*) children->item(i)));	
	}
}
void cef_parser::handleUsecases(DOMElement* element)
{
	
	DOMNodeList* children = element->getElementsByTagName(tags.use_case);
	communication=new vector<cef_usecase*>();
	for(unsigned i=0;i<children->getLength();i++ )
	{
		communication->push_back(handleUsecase( (DOMElement*) children->item(i)));	
	}
}
void cef_parser::handleLinks(DOMElement* element )
{
	
	DOMNodeList* children = element->getElementsByTagName(tags.link);
	links=new vector<cef_link*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		links->push_back(handleLink( (DOMElement*) children->item(i)));	
	}

}

string cef_parser::getText(DOMElement* element)
{
	string acc;
	DOMNodeList* children = element->getChildNodes();
	const char* whitespace = " \t\r\n\v\f";
	for(unsigned i=0;i<children->getLength();i++)
	{
		if(children->item(i)->getNodeType() == DOMNode::TEXT_NODE)			
		{
			acc=sm->convert(( (DOMText*) children->item(i))->getWholeText());	
			break;
		}
	}
	acc.erase(acc.find_last_not_of(whitespace) + 1);
	acc=acc.substr(acc.find_first_not_of(whitespace),acc.length());
	return acc;
}


	
	
	//Subroutines
cef* cef_parser::handleCEF(DOMElement* element)
{
	
	DOMNodeList* children = element->getElementsByTagName(tags.cef_version);
	double version = atof(getText( (DOMElement*) children->item(0)).c_str());	
	children = element->getElementsByTagName(tags.system);
	system=handleSystem( (DOMElement*) children->item(0));
	children = element->getElementsByTagName(tags.communication);
	communication=NULL;
	if(children->getLength()>0) handleUsecases( (DOMElement*) children->item(0));

	mycef=new cef();
	mycef->setVersion(version);
	mycef->setSystem(system);
	mycef->setCommunication(communication);
	return mycef;
}
cef_system* cef_parser::handleSystem(DOMElement* element)
{
	
	DOMNodeList* children = element->getElementsByTagName(tags.system_properties);
	system_properties=handleSystemProperties( (DOMElement*) children->item(0));
	children=element->getElementsByTagName(tags.blocks);
	handleBlocks((DOMElement*)children->item(0));
	children=element->getElementsByTagName(tags.domains);
	domains=NULL;
	if(children->getLength()>0) handleDomains((DOMElement*)children->item(0));
	children=element->getElementsByTagName(tags.links);
	links=NULL;
	if(children->getLength()>0) handleLinks((DOMElement*)children->item(0));
	children=element->getElementsByTagName(tags.routes);
	routes=NULL;
	if(children->getLength()>0) routes=handleRoutes((DOMElement*)children->item(0));
	
	return new cef_system(system_properties,domains,blocks,links,routes);
}

cef_system_properties* cef_parser::handleSystemProperties(DOMElement* element)
{
	cef_system_properties* sys_p;
	vector<cef_clock*>* clocks=new vector<cef_clock*>();
	vector<cef_powernet*>* power_nets=new vector<cef_powernet*>();
	pair<double,double>* fp_bbox=NULL;
	DOMNodeList* children = element->getElementsByTagName(tags.fp_bounding_box);
	string x="0";
	string y="0";
	if(children->getLength()>0)
	{	
		 x = getText((DOMElement*) ( ((DOMElement*)children->item(0))->getElementsByTagName(tags.fp_width))->item(0));
		 y = getText((DOMElement*) ( ((DOMElement*)children->item(0))->getElementsByTagName(tags.fp_height))->item(0));
		fp_bbox=new pair<double,double>(atof(x.c_str()),atof(y.c_str()));	
	}
	children = element->getElementsByTagName(tags.clocks);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.clock);	
	for(unsigned i=0;i<children->getLength();i++)
	{
		clocks->push_back(handleClock((DOMElement*)children->item(i)));
	}
	children = element->getElementsByTagName(tags.power_networks);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.power_network);	
	for(unsigned i=0;i<children->getLength();i++)
	{
		power_nets->push_back(handlePowerNet((DOMElement*)children->item(i)));
	}
	sys_p=new cef_system_properties(fp_bbox,clocks,power_nets);
	return sys_p;
}

cef_block* cef_parser::handleBlock(DOMElement* element)
{
	cef_block_orientation* orientation;
	cef_block_size*  size;
	unsigned layer,id,type;
	cef_block_blc_pos* blcpos;
	vector<cef_port*>* ports=new vector<cef_port*>();
	vector<unsigned>* conid=new vector<unsigned>();
	string name;
	cef_block_architecture* architecture;
	DOMNodeList* children = element->getElementsByTagName(tags.name);
	name=getText((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.id);
	id=atol(getText((DOMElement*)children->item(0)).c_str());
	children = element->getElementsByTagName(tags.block_type);
	type=atol(getText((DOMElement*)children->item(0)).c_str());

	children = element->getElementsByTagName(tags.connections);
	if(children->getLength()>0)
	{
		children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.connection_with_id);
		for(unsigned i=0;i<children->getLength();i++)
		{
			conid->push_back(atol(getText((DOMElement*)children->item(i)).c_str()));	
		}
	}	
	children = element->getElementsByTagName(tags.ports);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.port);
	for(unsigned i=0;i<children->getLength();i++)
	{
		ports->push_back(handlePort((DOMElement*)children->item(i)));	
	}
	children = element->getElementsByTagName(tags.size);
	size=NULL;
	if(children->getLength()>0)
		size=handleBlockSize((DOMElement*)children->item(0));
	blcpos=NULL;
	children = element->getElementsByTagName(tags.bottom_left_corner_position);
	if(children->getLength()>0)
		blcpos=handleBotLefCor((DOMElement*)children->item(0));
	orientation=NULL;
	children = element->getElementsByTagName(tags.orientation);
	if(children->getLength()>0)
		orientation=handleBlockOrientation((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.layer);
	layer=0;
	if(children->getLength()>0)
		layer=atol(getText((DOMElement*)children->item(0)).c_str());
	
	children = element->getElementsByTagName(tags.architecture);
	architecture=NULL;	
	if(children->getLength()>0)
		architecture=handleBlockArchitecture((DOMElement*)children->item(0));
	
	return new cef_block(id,name,type,conid,ports,size,orientation,blcpos,layer,architecture);	
	

}
cef_operating_point* cef_parser::handleOperatingPoint(DOMElement* element)
{
		
	string name = getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	double voltage= atol(getText( (DOMElement*) element->getElementsByTagName(tags.voltage)->item(0)).c_str());
	double frequency= atol(getText( (DOMElement*) element->getElementsByTagName(tags.frequency)->item(0)).c_str());
	return new cef_operating_point(id,name,voltage,frequency);
}
cef_domain* cef_parser::handleDomain(DOMElement* element)
{
	

	vector<cef_operating_point*>* ops=new vector<cef_operating_point*>();		
	string name = getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	unsigned clock_id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.clock_id)->item(0)).c_str());
	unsigned power_network_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.power_network_id)->item(0)).c_str());
	//bool shutdownallowed=toBool(getText( (DOMElement*) element->getElementsByTagName(tags.shutdownallowed)->item(0)));
	DOMNodeList* children = element->getElementsByTagName(tags.operating_points);
	if(children->getLength()>0)
	{
		children=  ((DOMElement*)children->item(0))->getElementsByTagName(tags.operating_point);
		for(unsigned i=0;i<children->getLength();i++)
		{
				ops->push_back(handleOperatingPoint((DOMElement*)children->item(i)));	
		}
	}
        return new cef_domain(id,name,clock_id,power_network_id,ops);
}
cef_link* cef_parser::handleLink(DOMElement* element)
{
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	unsigned source_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.source_port_id)->item(0)).c_str());
	unsigned dest_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.destination_port_id)->item(0)).c_str());
	bool carries_src_clk=false; 
	if(element->getElementsByTagName(tags.carries_source_clock)->getLength()>0)
		carries_src_clk=toBool(getText( (DOMElement*) element->getElementsByTagName(tags.carries_source_clock)->item(0)));
	bool carries_src_rst=false;
	if(element->getElementsByTagName(tags.carries_source_reset)->getLength()>0)
		carries_src_rst= toBool(getText( (DOMElement*) element->getElementsByTagName(tags.carries_source_reset)->item(0)));
	double length=-1;
	if(element->getElementsByTagName(tags.link_length_estimation)->getLength()>0)
		length=atof(getText( (DOMElement*) element->getElementsByTagName(tags.link_length_estimation)->item(0)).c_str());
	
	unsigned aux_f=0;
	if(element->getElementsByTagName(tags.auxilary_forward_wires)->getLength()>0)
		aux_f=	atol(getText( (DOMElement*) element->getElementsByTagName(tags.auxilary_forward_wires)->item(0)).c_str());
	
	unsigned aux_b=0;
	if(element->getElementsByTagName(tags.auxilary_forward_wires)->getLength()>0)
		aux_b=atol(getText( (DOMElement*) element->getElementsByTagName(tags.auxilary_backward_wires)->item(0)).c_str());

	vector<unsigned>* bll=new vector<unsigned>();
	DOMNodeList* children = element->getElementsByTagName(tags.blocks_on_link);
	if(children->getLength()>0)
	{
		children= ((DOMElement*)children->item(0))->getElementsByTagName(tags.block_on_link);
		for(unsigned i=0;i<children->getLength();i++)
		{
			bll->push_back(atol(getText( (DOMElement*) children->item(i)).c_str()));
		}
	}

	return new cef_link(name,id,source_port_id,dest_port_id,carries_src_clk,carries_src_rst,length,aux_f,aux_b,bll);
}
cef_flow* cef_parser::handleFlow(DOMElement* element)
{
	
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	string type = getText( (DOMElement*) element->getElementsByTagName(tags.flow_type)->item(0));
	unsigned mot= atol(getText( (DOMElement*) element->getElementsByTagName(tags.max_outstanding_transactions)->item(0)).c_str());
	double peak_bw= atof(getText( (DOMElement*) element->getElementsByTagName(tags.peak_bandwidth)->item(0)).c_str());
	double sust_bw= atof(getText( (DOMElement*) element->getElementsByTagName(tags.sustained_bandwidth)->item(0)).c_str());
	double zllb= atof(getText( (DOMElement*) element->getElementsByTagName(tags.zero_load_latency_bound)->item(0)).c_str());
	double lb= atof(getText( (DOMElement*) element->getElementsByTagName(tags.latency_bound)->item(0)).c_str());
	unsigned priority=0;
	bool prioritized=false;
	if(element->getElementsByTagName(tags.priority_class)->getLength()>0)
	{
		priority= atol(getText( (DOMElement*) element->getElementsByTagName(tags.priority_class)->item(0)).c_str());
		prioritized=true;
	}
	int gid = atoi(getText( (DOMElement*) element->getElementsByTagName(tags.gid)->item(0)).c_str());
	vector<cef_burst*>* burstiness=new vector<cef_burst*>();
	DOMNodeList* children = element->getElementsByTagName(tags.burstiness);
	if(children->getLength()>0)
	{
		children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.burst);
		for(unsigned i=0;i<children->getLength();i++)
		{
			burstiness->push_back(handleBurst((DOMElement*)children->item(i)));
		}
	}
	vector<cef_burst_trans*>* btransfers=new vector<cef_burst_trans*>();
	children = element->getElementsByTagName(tags.burst_transfers);
	if(children->getLength()>0)
	{
		children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.burst_transfer);
		for(unsigned i=0;i<children->getLength();i++)
		{
			btransfers->push_back(handleBurstTrans((DOMElement*)children->item(i)));
		}
	}

	return new cef_flow(id,type,mot,peak_bw,sust_bw,zllb,lb,gid,prioritized,priority,btransfers,burstiness);
}

cef_burst* cef_parser::handleBurst(DOMElement* element)
{
		
	unsigned length= atol(getText( (DOMElement*) element->getElementsByTagName(tags.length)->item(0)).c_str());
	double percent = atof(getText( (DOMElement*) element->getElementsByTagName(tags.percent)->item(0)).c_str());
	return new cef_burst(length,percent);

}

cef_burst_trans* cef_parser::handleBurstTrans(DOMElement* element)
{
	
	unsigned transfer_size= atol(getText( (DOMElement*) element->getElementsByTagName(tags.transfer_size)->item(0)).c_str());
	unsigned period= atol(getText( (DOMElement*) element->getElementsByTagName(tags.period)->item(0)).c_str());
	unsigned transfer_offset= atol(getText( (DOMElement*) element->getElementsByTagName(tags.transfer_offset)->item(0)).c_str());
	unsigned transfer_duration= atol(getText( (DOMElement*) element->getElementsByTagName(tags.transfer_duration)->item(0)).c_str());
	return new cef_burst_trans(transfer_size,period,transfer_offset,transfer_duration);
}

cef_usecase* cef_parser::handleUsecase(DOMElement* element)
{
		
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	DOMNodeList* children = element->getElementsByTagName(tags.domain_operating_points);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.domain);
	unsigned domain_id;
	unsigned operating_point_id;
	vector<pair<unsigned,unsigned>*>* dops=new vector<pair<unsigned,unsigned>*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		domain_id = atol(getText( (DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.domain_id)->item(0)).c_str());
		operating_point_id= atol(getText( (DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.operating_point_id)->item(0)).c_str());
		dops->push_back(new pair<unsigned,unsigned>(domain_id,operating_point_id));
	}
	double weight=1;
	children= element->getElementsByTagName(tags.weight);
	if(children->getLength()>0)
		weight = atof(getText( (DOMElement*) children->item(0)).c_str());
	children = element->getElementsByTagName(tags.initiators);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.initiator);
	vector<cef_initiator*>* initiators=new vector<cef_initiator*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		initiators->push_back(handleInitiator((DOMElement*)children->item(i)));	
	}	
	return new cef_usecase(name,id,dops,weight,initiators);
}

cef_port* cef_parser::handlePort(DOMElement* element)	
{
		
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	string protocol=getText( (DOMElement*) element->getElementsByTagName(tags.protocol)->item(0));
	string protocol_variant="";
	if(element->getElementsByTagName(tags.protocol_variant)->getLength()>0)
	{
		protocol_variant=getText( (DOMElement*) element->getElementsByTagName(tags.protocol_variant)->item(0));
	}
	unsigned mot=0;	
	if(element->getElementsByTagName(tags.max_outstanding_transactions)->getLength()>0)
	{
		mot= atol(getText( (DOMElement*) element->getElementsByTagName(tags.max_outstanding_transactions)->item(0)).c_str());
	}
	unsigned add_w=0;
	if(element->getElementsByTagName(tags.address_width)->getLength()>0)
		add_w= atol(getText( (DOMElement*) element->getElementsByTagName(tags.address_width)->item(0)).c_str());
	unsigned read_w=0; 
	if(element->getElementsByTagName(tags.read_data_width)->getLength()>0)
		read_w=atol(getText( (DOMElement*) element->getElementsByTagName(tags.read_data_width)->item(0)).c_str());

	unsigned write_w=0;
	if(element->getElementsByTagName(tags.write_data_width)->getLength()>0)
	 	write_w=atol(getText( (DOMElement*) element->getElementsByTagName(tags.write_data_width)->item(0)).c_str());

	unsigned flit_w=0;
	if(element->getElementsByTagName(tags.flit_width)->getLength()>0)
		flit_w=atol(getText( (DOMElement*) element->getElementsByTagName(tags.flit_width)->item(0)).c_str());

	double pos_x=0; 
	double pos_y=0;
	if(element->getElementsByTagName(tags.position_x)->getLength()>0)
		pos_x=atof(getText( (DOMElement*) element->getElementsByTagName(tags.position_x)->item(0)).c_str());
	if(element->getElementsByTagName(tags.position_y)->getLength()>0)
		pos_y=atof(getText( (DOMElement*) element->getElementsByTagName(tags.position_y)->item(0)).c_str());
	unsigned domain_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.domain_id)->item(0)).c_str());
	return new cef_port(id,protocol,protocol_variant,mot,add_w,read_w,write_w,flit_w,new pair<double,double>(pos_x,pos_y),domain_id);
	
}
cef_routes* cef_parser::handleRoutes(DOMElement* element)
{
	
	string routingpolicy=getText( (DOMElement*) element->getElementsByTagName(tags.routingpolicy)->item(0));
	DOMNodeList* children = element->getElementsByTagName(tags.source_routing);
	cef_sourcerouting* src=NULL;
	cef_distributedrouting* dst=NULL;
	cef_lbdr* lbdr=NULL;
	cef_fdor* fdor=NULL;
	vector<cef_virtual_network*>* vns=NULL;
	if(children->getLength()>0) src=handleSourceRouting((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.distributed_routing);
	if(children->getLength()>0) dst=handleDistributedRouting((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.lbdr);
	if(children->getLength()>0) lbdr=handleLBDR((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.fdor);
	if(children->getLength()>0) fdor=handleFDOR((DOMElement*)children->item(0));
	children = element->getElementsByTagName(tags.virtual_networks);
	if(children->getLength()>0)
	{
		vns=new vector<cef_virtual_network*>();
		children =((DOMElement*)children->item(0))->getElementsByTagName(tags.virtual_network);
		for(unsigned i=0;i<children->getLength();i++)
		{
			vns->push_back(handleVirtualNet((DOMElement*)children->item(i)));
		}
	}
	
	return new cef_routes(routingpolicy,src,dst,lbdr,fdor,vns);
	
}

cef_virtual_network* cef_parser::handleVirtualNet(DOMElement* element)
{
		
	unsigned id = atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	vector<unsigned>* ids=new vector<unsigned>();
	DOMNodeList* children = element->getElementsByTagName(tags.vc_ids);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.vc_id);
	for(unsigned i=0;i<children->getLength();i++)
	{
		ids->push_back(atol(getText((DOMElement*)children->item(i)).c_str()));	
	}
	return new cef_virtual_network(id,ids);
}
cef_target* cef_parser::handleTarget(DOMElement* element)
{
		
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned target_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.target_port_id)->item(0)).c_str());
	vector<cef_flow*>* flows=new vector<cef_flow*>();
	DOMNodeList* children = element->getElementsByTagName(tags.flows);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.flow);
	for(unsigned i=0;i<children->getLength();i++)
	{
		flows->push_back(handleFlow((DOMElement*)children->item(i)));
	}
	
	return new cef_target(target_port_id,name,flows);
	
}
cef_initiator* cef_parser::handleInitiator(DOMElement* element)
{
		
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned initiator_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.initiator_port_id)->item(0)).c_str());
	vector<cef_target*>* targets=new vector<cef_target*>();
	DOMNodeList* children = element->getElementsByTagName(tags.targets);
	children = ((DOMElement*)children->item(0))->getElementsByTagName(tags.target);
	for(unsigned i=0;i<children->getLength();i++)
	{
		targets->push_back(handleTarget((DOMElement*)children->item(i)));
	}
	return new cef_initiator(initiator_port_id,name,targets);
}
cef_clock* cef_parser::handleClock(DOMElement* element)
{
	
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	unsigned skew=0;
	if(	element->getElementsByTagName(tags.skew)->getLength()>0)
	{
		skew= atol(getText( (DOMElement*) element->getElementsByTagName(tags.skew)->item(0)).c_str());
	}
	unsigned derived_from_id=0;
	unsigned derivation_type=0;
	int derivation_phase=0;
	bool isderived=false;

	vector<double>* frequency=new vector<double>();
	DOMNodeList* children=element->getElementsByTagName(tags.frequency);
	for(unsigned i=0;i<children->getLength();i++)
	{
		frequency->push_back(atol(getText( (DOMElement*) children->item(i)).c_str()));
	}
	children=element->getElementsByTagName(tags.derived_from_id);
	if(children->getLength()>0)
	{
		isderived=true;
		derived_from_id= atol(getText( (DOMElement*) children->item(0)).c_str());
		derivation_type= atol(getText( (DOMElement*) element->getElementsByTagName(tags.derivation_type)->item(0)).c_str());
		derivation_phase= atol(getText( (DOMElement*) element->getElementsByTagName(tags.derivation_phase)->item(0)).c_str());
	}
	cef_derivation_type x;
	switch(derivation_type)
	{
		case 2:
			x=ALIGN;
			break;
		case 1: 
			x=MESO;
			break;
		default:
			x=SYNC;
	}	
	
	return new cef_clock(name,id,frequency,skew,derived_from_id,x,derivation_phase,isderived);
}
cef_powernet* cef_parser::handlePowerNet(DOMElement* element)
{
	string name= getText((DOMElement*) element->getElementsByTagName(tags.name)->item(0));	
	unsigned id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.id)->item(0)).c_str());
	vector<double>* voltage=new vector<double>(); 
	DOMNodeList* children=element->getElementsByTagName(tags.voltage);
	for(unsigned i=0;i<children->getLength();i++)
	{
		voltage->push_back(atol(getText( (DOMElement*) children->item(i)).c_str()));
	}
	children=element->getElementsByTagName(tags.derived_from_id);
	unsigned derived_from_id=0;
	bool isderived=false;
	if(children->getLength()>0)
	{
		isderived=true;
		derived_from_id= atol(getText( (DOMElement*) children->item(0)).c_str());
	}
	return new cef_powernet(name,id,voltage,derived_from_id,isderived);
	
}
cef_block_size* cef_parser::handleBlockSize(DOMElement* element)
{
	
	bool soft= toBool(getText((DOMElement*) element->getElementsByTagName(tags.soft)->item(0)));
	DOMNodeList* children=element->getElementsByTagName(tags.shape);
	vector<pair<double,double>*>* shape=NULL;
	if(children->getLength()>0)
	{
		children=((DOMElement*)children->item(0))->getElementsByTagName(tags.corners);
		children=((DOMElement*)children->item(0))->getElementsByTagName(tags.corner);
		double x,y;	
		shape=new vector<pair<double,double>*>();
		for(unsigned i=0;i<children->getLength();i++)
		{
			
			x=atof(getText((DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.x)->item(0)).c_str());
			y=atof(getText((DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.y)->item(0)).c_str());
			shape->push_back(new pair<double,double>(x,y));
		}
	}
	double area=atof(getText((DOMElement*) element->getElementsByTagName(tags.area)->item(0)).c_str());
	children=element->getElementsByTagName(tags.aspectratio);
	double ar=0;
	if(children->getLength()>0)
	{
		ar==atof(getText((DOMElement*) element->getElementsByTagName(tags.aspectratio)->item(0)).c_str());

	}
	return new cef_block_size(soft,shape,area,ar);
	
}
cef_block_architecture* cef_parser::handleBlockArchitecture(DOMElement* element)
{
	string ni_type="none";	
	if(element->getElementsByTagName(tags.ni_type)->getLength()>0)	
		ni_type= getText((DOMElement*) element->getElementsByTagName(tags.ni_type)->item(0));	
	vector<pair<unsigned long long,unsigned long long>*>*  addr= new vector<pair<unsigned long long, unsigned long long>*>();
	unsigned depth=0;
	vector<unsigned>* input=new vector<unsigned>();
	vector<unsigned>* output=new vector<unsigned>();
	unsigned long long start=0;
	unsigned long long size=0;
	DOMNodeList* children=element->getElementsByTagName(tags.address_ranges);
	if(children->getLength()>0)
	{
		children=((DOMElement*)children->item(0))->getElementsByTagName(tags.address_range);
		for(unsigned i=0;i<children->getLength();i++)
		{
						
			start=atol(getText((DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.start)->item(0)).c_str());
			size=atol(getText((DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.size)->item(0)).c_str());
			addr->push_back(new pair<unsigned long long,unsigned long long>(start,size));
		}
	}
	children=element->getElementsByTagName(tags.input_buffers);
	if(children->getLength()>0)
	{
		children=((DOMElement*)children->item(0))->getElementsByTagName(tags.depth);
		for(unsigned i=0;i<children->getLength();i++)
		{
						
			depth=atol(getText((DOMElement*) ((DOMElement*)children->item(i))).c_str());
			input->push_back(depth);
		}
	}
	children=element->getElementsByTagName(tags.output_buffers);
	if(children->getLength()>0)
	{
		children=((DOMElement*)children->item(0))->getElementsByTagName(tags.depth);
		for(unsigned i=0;i<children->getLength();i++)
		{
						
			depth=atol(getText((DOMElement*) ((DOMElement*)children->item(i))).c_str());
			output->push_back(depth);
		}
	}	
	unsigned vcs=0;
	if(element->getElementsByTagName(tags.virtual_channels)->getLength()>0)
	{	
	    vcs=atol(getText( (DOMElement*) element->getElementsByTagName(tags.virtual_channels)->item(0)).c_str());
	}
	string vcas="";
	string ap="";
	if(element->getElementsByTagName(tags.arbitration_policy)->getLength()>0)//TODO:
		ap= getText((DOMElement*) element->getElementsByTagName(tags.arbitration_policy)->item(0));	
	string vcap=""; 
	if(vcs>1)
	{
		vcas= getText((DOMElement*) element->getElementsByTagName(tags.virtual_channel_arbitration_structure)->item(0));	
		vcap=getText((DOMElement*) element->getElementsByTagName(tags.virtual_channel_arbitration_policy)->item(0));	
	}

	return new cef_block_architecture(ni_type,addr,input,output,vcs,vcas,ap,vcap);	
}
cef_block_blc_pos* cef_parser::handleBotLefCor(DOMElement* element )
{
	
	double x=atof(getText((DOMElement*) element->getElementsByTagName(tags.x)->item(0)).c_str());
	double y=atof(getText((DOMElement*) element->getElementsByTagName(tags.y)->item(0)).c_str());
	bool movable= toBool(getText((DOMElement*) element->getElementsByTagName(tags.movable)->item(0)));
	double initx=atof(getText((DOMElement*) element->getElementsByTagName(tags.initial_x)->item(0)).c_str());
	double inity=atof(getText((DOMElement*) element->getElementsByTagName(tags.initial_y)->item(0)).c_str());
	return new cef_block_blc_pos(x,y,movable,initx,inity);
}
cef_block_orientation* cef_parser::handleBlockOrientation(DOMElement* element)
{
	
	bool rotatable= toBool(getText((DOMElement*) element->getElementsByTagName(tags.rotatable)->item(0)));
	bool mirrorable= toBool(getText((DOMElement*) element->getElementsByTagName(tags.mirrorable)->item(0)));
	cef_degree_orientation rotation;
	unsigned rot=atol(getText( (DOMElement*) element->getElementsByTagName(tags.rotation)->item(0)).c_str());
	cef_degree_orientation initial_rotation;
	unsigned init_rot=  atol(getText( (DOMElement*) element->getElementsByTagName(tags.initial_rotation)->item(0)).c_str());
	switch(rot)
	{
		case 90:	
			rotation=d90;
			break;
		case 180:
			rotation=d180;
			break;
		case 270:
			rotation=d270;
			break;
		default:
			rotation=d0;
			break;
	}	
	switch(init_rot)
	{
		case 90:	
			initial_rotation=d90;
			break;
		case 180:
			initial_rotation=d180;
			break;
		case 270:
			initial_rotation=d270;
			break;
		default:
			initial_rotation=d0;
			break;
	}	

	bool mirroring_x= toBool(getText((DOMElement*) element->getElementsByTagName(tags.mirroring_x)->item(0)));
	bool init_mirroring_x= toBool(getText((DOMElement*) element->getElementsByTagName(tags.initial_mirroring_x)->item(0)));
	bool mirroring_y= toBool(getText((DOMElement*) element->getElementsByTagName(tags.mirroring_y)->item(0)));
	bool init_mirroring_y= toBool(getText((DOMElement*) element->getElementsByTagName(tags.initial_mirroring_y)->item(0)));
	return new cef_block_orientation(rotatable,mirrorable,rotation,initial_rotation,mirroring_x,init_mirroring_x,mirroring_y,init_mirroring_y);
}
cef_sourcerouting* cef_parser::handleSourceRouting(DOMElement* element)
{
	vector<cef_src_route*>* routes=new vector<cef_src_route*>();	
	DOMNodeList* children=element->getElementsByTagName(tags.route);
	DOMNodeList* children2;
	unsigned source_id;
	unsigned dest_id;
	vector<unsigned>* swopi;
	double route_weight;
	for(unsigned i=0;i<children->getLength();i++)
	{
		source_id= atol(getText( (DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.source_id)->item(0)).c_str());
		dest_id= atol(getText( (DOMElement*) ((DOMElement*)children->item(i))->getElementsByTagName(tags.destination_id)->item(0)).c_str());
		children2=((DOMElement*)children->item(i))->getElementsByTagName(tags.switch_output_port_ids);
	 	children2=((DOMElement*)children2->item(0))->getElementsByTagName(tags.switch_output_port_id);
		swopi=new vector<unsigned>();
		for(unsigned j=0;j<children2->getLength();j++)
		{
			swopi->push_back(atol(getText((DOMElement*)children2->item(j)).c_str()));
		}
		children2=((DOMElement*)children->item(i))->getElementsByTagName(tags.route_weight);
		
		if(children2->getLength()>0)
		{
			route_weight=atof(getText((DOMElement*) children2->item(0)).c_str());
		}
		else
			route_weight=1;
		
		routes->push_back(new cef_src_route(source_id,dest_id,swopi,route_weight));
	}
	return new cef_sourcerouting(routes);
	
}
cef_distributedrouting* cef_parser::handleDistributedRouting(DOMElement* element)
{
	vector<cef_routing_table*>* tables=new vector<cef_routing_table*>();
	DOMNodeList* children=element->getElementsByTagName(tags.tables);
	DOMNodeList* children2;
	DOMElement* table;
	children=((DOMElement*)children->item(0))->getElementsByTagName(tags.table);
	unsigned switch_id;
	unsigned dest_id;
	unsigned input_port_id;
	unsigned id;
	double w;
	vector<pair<unsigned,double>* >* output_ports;
	for(unsigned i=0;i<children->getLength();i++)
	{
		table=(DOMElement*)children->item(i);
		switch_id=atol(getText( (DOMElement*) table->getElementsByTagName(tags.switch_id)->item(0)).c_str());
		dest_id=atol(getText( (DOMElement*) table->getElementsByTagName(tags.destination_id)->item(0)).c_str());
		input_port_id=atol(getText( (DOMElement*) table->getElementsByTagName(tags.input_port_id)->item(0)).c_str());
		children2=table->getElementsByTagName(tags.output_ports);
		children2=((DOMElement*)children2->item(0))->getElementsByTagName(tags.output_port);
		output_ports=new vector<pair<unsigned,double>*>();
		for(unsigned j=0;j<children2->getLength();j++)
		{
					
			id=atol(getText( (DOMElement*) ((DOMElement*)children2->item(j))->getElementsByTagName(tags.output_port_id)->item(0)).c_str());
			if(((DOMElement*)children2->item(j))->getElementsByTagName(tags.route_weight)->getLength()>0)
			{
				w=atol(getText( (DOMElement*) ((DOMElement*)children2->item(j))->getElementsByTagName(tags.route_weight)->item(0)).c_str());
			}
			else
				w=1;
			output_ports->push_back(new pair<unsigned,double>(id,w));
		}
		tables->push_back(new cef_routing_table(switch_id,dest_id,input_port_id,output_ports));
	}
	return new cef_distributedrouting(tables);
}
cef_lbdr* cef_parser::handleLBDR(DOMElement* element)
{
	
	DOMNodeList* children=element->getElementsByTagName(tags.lbdr_entry);
	vector<cef_lbdr_entry*>* entries=new vector<cef_lbdr_entry*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		entries->push_back(handleLBDREntry((DOMElement*)children->item(i)));	
	}
	return new cef_lbdr(entries);
}
cef_lbdr_entry* cef_parser::handleLBDREntry(DOMElement* element)
{
	
	unsigned switch_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.switch_id)->item(0)).c_str());
	unsigned long long lbdr_routing_bits= str2u(getText( (DOMElement*) element->getElementsByTagName(tags.lbdr_routing_bits)->item(0)));
	unsigned long long lbdr_con_bits= str2u(getText( (DOMElement*) element->getElementsByTagName(tags.lbdr_connectivity_bits)->item(0)));
	DOMNodeList* children=element->getElementsByTagName(tags.lbdr_deroutes);
	children=((DOMElement*)children->item(0))->getElementsByTagName(tags.lbdr_deroute);
	vector<pair<unsigned,unsigned>*>* deroutes=new vector<pair<unsigned,unsigned>*>();
	unsigned input_port_id,deroute_port;
	for(unsigned i=0;i<children->getLength();i++)
	{
		input_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.input_port_id)->item(0)).c_str());
		deroute_port= atol(getText( (DOMElement*) element->getElementsByTagName(tags.deroute_port)->item(0)).c_str());
		deroutes->push_back(new pair<unsigned,unsigned>(input_port_id,deroute_port));
	}
	children=element->getElementsByTagName(tags.lbdr_forks);
	children=((DOMElement*)children->item(0))->getElementsByTagName(tags.lbdr_fork);
	vector<cef_lbdr_fork*>* forks=new vector<cef_lbdr_fork*>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		forks->push_back(handleLBDRFork((DOMElement*)children->item(0)));	
	}	
	return new cef_lbdr_entry(switch_id,lbdr_routing_bits,lbdr_con_bits,deroutes,forks);	
}
cef_lbdr_fork* cef_parser::handleLBDRFork(DOMElement* element)
{
		
	unsigned input_port_id= atol(getText( (DOMElement*) element->getElementsByTagName(tags.input_port_id)->item(0)).c_str());
	
	DOMNodeList* children=element->getElementsByTagName(tags.fork_ports);
	children=((DOMElement*)children->item(0))->getElementsByTagName(tags.fork_port);
	vector<unsigned>* ports=new vector<unsigned>();
	for(unsigned i=0;i<children->getLength();i++)
	{
		ports->push_back(atol(getText( (DOMElement*) children->item(i)).c_str()));	
	}
	return new cef_lbdr_fork(input_port_id,ports);

}
cef_fdor* cef_parser::handleFDOR(DOMElement* element)
{
	vector<pair<unsigned,unsigned long long>*>* entries=new vector<pair<unsigned,unsigned long long>*>();	
	DOMNodeList* children=element->getElementsByTagName(tags.fdor_entry);
	unsigned a;
	unsigned long long b;
	for(unsigned i=0;i<children->getLength();i++)
	{
	
		a= atol(getText( (DOMElement*) element->getElementsByTagName(tags.switch_id)->item(0)).c_str());
		b= atoll(getText( (DOMElement*) element->getElementsByTagName(tags.fdor_bits)->item(0)).c_str());
		entries->push_back(new pair<unsigned,unsigned long long>(a,b));
	}
	return new cef_fdor(entries);	
	
}
//DOMElement* cef_parser::inverseCEF(cef* c)
//{
//	DOMElement* 
//
//}
	
	//Reverse routines
//	DOMElement* getBlocks(std::vector<cef_block*>*);
//	DOMElement* getLinks(std::vector<cef_link*>*);
//	DOMElement* getRoutes(cef_routes*);
//	DOMElement* getSystem(cef_system*);
//	DOMElement* getSystemProperties(cef_system_properties*);
//	DOMElement* getDomains(std::vector<cef_domain*>*);
//	DOMElement* getFlows(std::vector<cef_flow*>*);
//	
//	//Reverse subroutines	
//	DOMElement* getPort(cef_port*);	
//	DOMElement* getBlock(cef_block*);
//	DOMElement* getDomain(cef_domain*);
//	DOMElement* getLink(cef_link*);
//	DOMElement* getFlow(cef_flow*);
//	DOMElement* getTarget(cef_target*);
//	DOMElement* getInitiator(cef_initiator*);
//	DOMElement* getClock(cef_clock*);
//	DOMElement* getPowerNet(cef_powernet*);
//	DOMElement* getConnection(cef_connection*);
//	DOMElement* getBlockSize(cef_block_size*);
//	DOMElement* getBlockArchitecture(cef_block_architecture*);
//	DOMElement* getBotLefCor(cef_block_blc_pos*);
//	DOMElement* getBlockOrientation(cef_block_orientation*);
//	DOMElement* getSourceRouting(cef_sourcerouting*);
//	DOMElement* getDistributedRouting(cef_distributedrouting*);
//	DOMElement* getRoutingTable(cef_routing_table*);
//	DOMElement* getLBDR(cef_lbdr*);
//	DOMElement* getLBDREntry(cef_lbdr_entry*);
//	DOMElement* getFDOR(cef_fdor*);
//	DOMElement* getUsecase(cef_usecase*);
	


DOMDocument* cef_parser::newCEF()
{
	const char* mem="<cef></cef>";
	const XMLCh* gMemBufId=sm->convert(mem);
	MemBufInputSource *memBufIS = new MemBufInputSource((const XMLByte*)mem, strlen(mem), gMemBufId, false);

	myparser.setValidationScheme( xercesc::XercesDOMParser::Val_Never ) ;
	myparser.setDoNamespaces( false ) ;
	myparser.setDoSchema( false ) ;
	myparser.setLoadExternalDTD( false ) ;
	myparser.parse( *memBufIS ) ;
	
	return myparser.adoptDocument();
}


void cef_parser::writeXML(cef* wcef ,string filePath)
{
	stringstream s;
	wDoc=newCEF();	
	DOMElement* cef_version = wDoc->createElement(tags.cef_version);
	s<<wcef->getVersion();
	DOMText* cef_v_t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	cef_version->appendChild(cef_v_t);
	DOMElement* root=wDoc->getDocumentElement();
	root->appendChild(cef_version);
	if(wcef->getCommunication())
	{
	DOMElement* _communication = convertCommunication(wcef->getCommunication());
	root->appendChild(_communication);
	}
	DOMElement* _system = convertSystem(wcef->getSystem());
	root->appendChild(_system);

  DOMImplementation *implementation = DOMImplementationRegistry::getDOMImplementation(sm->convert("LS"));

    // Create a DOMLSSerializer which is used to serialize a DOM tree into an XML document.
    DOMLSSerializer *serializer = ((DOMImplementationLS*)implementation)->createLSSerializer();

    // Make the output more human readable by inserting line feeds.
    if (serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
        serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);

    // The end-of-line sequence of characters to be used in the XML being written out. 
    serializer->setNewLine(XMLString::transcode("\r\n")); 

    // Convert the path into Xerces compatible XMLCh*.
    XMLCh *tempFilePath = sm->convert(filePath.c_str());

    // Calculate the length of the string.
    const int pathLen = XMLString::stringLen(tempFilePath);

    // Allocate memory for a Xerces string sufficent to hold the path.
    XMLCh *targetPath = (XMLCh*)XMLPlatformUtils::fgMemoryManager->allocate((pathLen + 9) * sizeof(XMLCh));

    // Fixes a platform dependent absolute path filename to standard URI form.
    XMLString::fixURI(tempFilePath, targetPath);

    // Specify the target for the XML output.
    XMLFormatTarget *formatTarget = new LocalFileFormatTarget(targetPath);
    //XMLFormatTarget *myFormTarget = new StdOutFormatTarget();

    // Create a new empty output destination object.
    DOMLSOutput *output = ((DOMImplementationLS*)implementation)->createLSOutput();

    // Set the stream to our target.
    output->setByteStream(formatTarget);

    // Write the serialized output to the destination.
    serializer->write(wDoc, output);

    serializer->release();
    delete formatTarget;
    output->release();
}

DOMElement* cef_parser::convertCommunication(vector<cef_usecase*>* com)
{
	DOMElement* _communication=wDoc->createElement(tags.communication);
	DOMElement* _usecase;
	for(unsigned i=0;i<com->size();i++)
	{
		_usecase=convertUsecase((*com)[i]);	
		_communication->appendChild(_usecase);	
	}
	return _communication;
		
}
DOMElement* cef_parser::convertUsecase(cef_usecase* use)
{
	stringstream s;
	DOMElement* _usecase=wDoc->createElement(tags.use_case);
	DOMElement* _name=wDoc->createElement(tags.name);	
	DOMText* t_name=wDoc->createTextNode(sm->convert(use->getName().c_str()));
	_name->appendChild(t_name);
	_usecase->appendChild(_name);
	s<<use->getID();
	DOMElement* _id=wDoc->createElement(tags.id);
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);	
	_usecase->appendChild(_id);
	DOMElement* _dops=wDoc->createElement(tags.domain_operating_points);
	DOMElement* _dop;
	DOMElement* _did;
	DOMElement* _opi;
	DOMText* t_did,*t_opi;
	vector<pair<unsigned,unsigned>*>* ops=use->getDOPs();
	for(unsigned i=0;i<ops->size();i++)
	{
		_dop=wDoc->createElement(tags.domain);	
		s.str("");
		s.clear();
		s<<(*ops)[i]->first;
		_did=wDoc->createElement(tags.domain_id);
		t_did=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_did->appendChild(t_did);
		s.str("");
		s.clear();
		s<<(*ops)[i]->second;
		_opi=wDoc->createElement(tags.operating_point_id);
		t_opi=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_opi->appendChild(t_opi);
		_dop->appendChild(_did);
		_dop->appendChild(_opi);
		_dops->appendChild(_dop);
	}
	_usecase->appendChild(_dop);
	if(use->getWeight()>0) 
	{
		DOMElement* _weight = wDoc->createElement(tags.weight);
		s.str("");
		s.clear();
		s<<use->getWeight();
		DOMText* t_weight= wDoc->createTextNode(sm->convert(s.str().c_str()));
		_weight->appendChild(t_weight);	
		_usecase->appendChild(_weight);
	}
	DOMElement* _initiators=wDoc->createElement(tags.initiators);
	DOMElement* _initiator;
	vector<cef_initiator*>* inits=use->getInitiators();
	for(unsigned i=0;i<inits->size();i++)
	{
		_initiator=convertInitiator((*inits)[i]);
		_initiators->appendChild(_initiator);
		
	}
	_usecase->appendChild(_initiators);
	return _usecase;
}
DOMElement* cef_parser::convertInitiator(cef_initiator* init)
{
	stringstream s;
	DOMElement* _initiator=wDoc->createElement(tags.initiator);
	DOMElement* _name=wDoc->createElement(tags.name);	
	DOMText* t_name=wDoc->createTextNode(sm->convert(init->getName().c_str()));	
	_name->appendChild(t_name);
	_initiator->appendChild(_name);
	DOMElement* _ipid=wDoc->createElement(tags.initiator_port_id);
	s<<init->getID();
	DOMText* t_ipid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_ipid->appendChild(t_ipid);
	_initiator->appendChild(_ipid);
	DOMElement* _targets=wDoc->createElement(tags.targets);
	DOMElement* _target;
	vector<cef_target*>* targets=init->getTargets();

	for(unsigned i=0;i<targets->size();i++)
	{
		_target=convertTarget((*targets)[i]);	
		_targets->appendChild(_target);
	}	
	_initiator->appendChild(_targets);	
	return _initiator;
	
}

DOMElement* cef_parser::convertTarget(cef_target* tar)
{
	stringstream s;
	DOMElement* _target=wDoc->createElement(tags.target);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(tar->getName().c_str()));
	_name->appendChild(t_name);
	_target->appendChild(_name);
	DOMElement* _tpd=wDoc->createElement(tags.target_port_id);
	s<<tar->getID();
	DOMText* t_tpd=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_tpd->appendChild(t_tpd);
	_target->appendChild(_tpd);
	DOMElement* _flows=wDoc->createElement(tags.flows); 
	DOMElement* _flow;
	vector<cef_flow*>* flows=tar->getFlows();
	for(unsigned i=0;i<flows->size();i++)
	{
		_flow=convertFlow((*flows)[i]);
		_flows->appendChild(_flow);
	}
	_target->appendChild(_flows);
	return _target;
}

DOMElement* cef_parser::convertFlow(cef_flow* fl)
{
	stringstream s;
	DOMElement* _flow=wDoc->createElement(tags.flow);
	DOMElement* _id=wDoc->createElement(tags.id);
	s<<fl->getID();	
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_flow->appendChild(_id);
//
	DOMElement* _ft=wDoc->createElement(tags.flow_type);
	DOMText* t_ft=wDoc->createTextNode(sm->convert(fl->getType().c_str()));
	_ft->appendChild(t_ft);
	_flow->appendChild(_ft);
//
	DOMElement* _mot=wDoc->createElement(tags.max_outstanding_transactions);
	s.str("");
	s.clear();
	s<<fl->getMOT();	
	DOMText* t_mot=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_mot->appendChild(t_mot);
	_flow->appendChild(_mot);
//
	DOMElement* _peakb=wDoc->createElement(tags.peak_bandwidth);
	s.str("");
	s.clear();
	s<<fl->getPeakBW();	
	DOMText* t_peakb=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_peakb->appendChild(t_peakb);
	_flow->appendChild(_peakb);
//	
	DOMElement* _sustb=wDoc->createElement(tags.sustained_bandwidth);
	s.str("");
	s.clear();
	s<<fl->getSustainedBW();	
	DOMText* t_sustb=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_sustb->appendChild(t_sustb);
	_flow->appendChild(_sustb);
//
	DOMElement* _zllb=wDoc->createElement(tags.zero_load_latency_bound);
	s.str("");
	s.clear();
	s<<fl->getZeroLoadLatB();	
	DOMText* t_zllb=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_zllb->appendChild(t_zllb);
	_flow->appendChild(_zllb);
//	
	DOMElement* _lb=wDoc->createElement(tags.latency_bound);
	s.str("");
	s.clear();
	s<<fl->getLatB();	
	DOMText* t_lb=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_lb->appendChild(t_lb);
	_flow->appendChild(_lb);
//
	if(fl->isPrioritized())
	{

		DOMElement* _pc=wDoc->createElement(tags.priority_class);
		s.str("");
		s.clear();
		s<<fl->getPriority();	
		DOMText* t_pc=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_pc->appendChild(t_pc);
		_flow->appendChild(_pc);
	}
	DOMElement* _gid=wDoc->createElement(tags.gid);
	s.str("");
	s.clear();
	s<<fl->getGID();	
	DOMText* t_gid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_gid->appendChild(t_gid);
	_flow->appendChild(_gid);
	if(fl->getBurstiness()!=NULL && fl->getBurstiness()->size()>0)
	{
		DOMElement* _bness=wDoc->createElement(tags.burstiness);	
		DOMElement* _burst;
		for(unsigned i=0;i<fl->getBurstiness()->size();i++)
		{
			_burst=convertBurst((*fl->getBurstiness())[i]);	
			_bness->appendChild(_burst);
		}
		_flow->appendChild(_bness);
	}
	if(fl->getBurstTrans()!=NULL && fl->getBurstTrans()->size()>0)
	{
			
		DOMElement* _btrans=wDoc->createElement(tags.burst_transfers);	
		DOMElement* _trans;
		for(unsigned i=0;i<fl->getBurstTrans()->size();i++)
		{
			_trans=convertBurstTrans((*fl->getBurstTrans())[i]);	
			_btrans->appendChild(_trans);
		}
		_flow->appendChild(_btrans);
	}
	return _flow;
}

DOMElement* cef_parser::convertBurst(cef_burst* bs)
{
	stringstream s;
	DOMElement* _burst=wDoc->createElement(tags.burst);	
	DOMElement* _len=wDoc->createElement(tags.length);
	s.str("");
	s.clear();
	s<<bs->getLength();	
	DOMText* t_len=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_len->appendChild(t_len);
	_burst->appendChild(_len);
//
	DOMElement* _per=wDoc->createElement(tags.percent);
	s.str("");
	s.clear();
	s<<bs->getPercent();	
	DOMText* t_per=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_per->appendChild(t_per);
	_burst->appendChild(_per);
	return _burst;
}

DOMElement* cef_parser::convertBurstTrans(cef_burst_trans* bt)
{
	
	stringstream s;
	DOMElement* _bt=wDoc->createElement(tags.burst_transfer);	
//
	DOMElement* _sz=wDoc->createElement(tags.transfer_size);
	s.str("");
	s.clear();
	s<<bt->getSize();	
	DOMText* t_sz=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_sz->appendChild(t_sz);
	_bt->appendChild(_sz);
//	
	DOMElement* _per=wDoc->createElement(tags.period);
	s.str("");
	s.clear();
	s<<bt->getPeriod();	
	DOMText* t_per=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_per->appendChild(t_per);
	_bt->appendChild(_per);
//	
	if(bt->getOffset()>0)
	{
		DOMElement* _off=wDoc->createElement(tags.transfer_offset);
		s.str("");
	s.clear();
		s<<bt->getOffset();	
		DOMText* t_off=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_off->appendChild(t_off);
		_bt->appendChild(_off);
	}
//
	if(bt->getDuration()>0)
	{
		DOMElement* _dur=wDoc->createElement(tags.transfer_duration);
		s.str("");
		s.clear();
		s<<bt->getDuration();	
		DOMText* t_dur=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_dur->appendChild(t_dur);
		_bt->appendChild(_dur);
	}
	return _bt;
}

DOMElement* cef_parser::convertSystem(cef_system* sys)
{
	DOMElement* _system=wDoc->createElement(tags.system);
	DOMElement* _system_prop=convertSystemProperties(sys->getProperties());
	_system->appendChild(_system_prop);
	DOMElement* _domains=wDoc->createElement(tags.domains);
	_system->appendChild(_domains);
	DOMElement* _blocks=wDoc->createElement(tags.blocks);
	_system->appendChild(_blocks);
	if(sys->getLinks()!=NULL && sys->getLinks()->size()>0)
	{
		DOMElement* _links=wDoc->createElement(tags.links);
		DOMElement* _link;	
		for(unsigned i=0;i<sys->getLinks()->size();i++)
		{
			_link=convertLink((*sys->getLinks())[i]);	
			_links->appendChild(_link);
		}	
		_system->appendChild(_links);
	}
	if(sys->getRoutes()!=NULL)
	{
		//DOMElement* _routes=handleRoutes(sys->getRoutes());
		DOMElement* _routes = convertRoutes(sys->getRoutes());
		_system->appendChild(_routes);
	}
	return _system;
}

DOMElement* cef_parser::convertSystemProperties(cef_system_properties* sys_p)
{
	stringstream s;
	DOMElement* _sysp=wDoc->createElement(tags.system_properties);
	if(sys_p->getBoundingBox()!=NULL)
	{
		DOMElement* _fpb=wDoc->createElement(tags.fp_bounding_box);
		DOMElement* _fpw=wDoc->createElement(tags.fp_width);
		s.str("");
		s.clear();
		s<<sys_p->getBoundingBox()->first;
		DOMText* t_fpw=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_fpw->appendChild(t_fpw);
		DOMElement* _fph=wDoc->createElement(tags.fp_height);
		s.str("");
		s.clear();
		s<<sys_p->getBoundingBox()->first;
		DOMText* t_fph=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_fph->appendChild(t_fph);
		_fpb->appendChild(_fpw);
		_fpb->appendChild(_fph);
		_sysp->appendChild(_fpb);
	}
	DOMElement* _clocks=wDoc->createElement(tags.clocks);
	DOMElement* _clock;
	vector<cef_clock*>* clocks=sys_p->getClocks();
	for(unsigned i=0;i<clocks->size();i++)
	{
		_clock=convertClock((*clocks)[i]);	
		_clocks->appendChild(_clock);
	}
	_sysp->appendChild(_clocks);
	DOMElement* _pnets=wDoc->createElement(tags.power_networks);
	DOMElement* _pnet;
	vector<cef_powernet*>* powernets=sys_p->getPowerNets();
	for(unsigned i=0;i<powernets->size();i++)
	{
		_pnet=convertPowerNet((*powernets)[i]);	
		_pnets->appendChild(_pnet);
	}
	_sysp->appendChild(_pnets);
	return _sysp;
}

DOMElement* cef_parser::convertClock(cef_clock* c)
{
	stringstream s;
	vector<double>* frequency;
	DOMElement* _clock=wDoc->createElement(tags.clock);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(c->getName().c_str()));
	_name->appendChild(t_name);
	_clock->appendChild(_name);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<c->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_clock->appendChild(_id);

	DOMElement* _f;
	DOMText* t_f;
	frequency=c->getFrequency();
	for(unsigned i=0;i<frequency->size();i++)	
	{
		_f=wDoc->createElement(tags.frequency);
		s.str("");
		s.clear();
		s<<(*frequency)[i];
		t_f=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_f->appendChild(t_f);
		_clock->appendChild(_f);
	}
	if(c->getSkew()!=0)
	{
		DOMElement* _sk=wDoc->createElement(tags.skew);
		s.str("");
		s.clear();
		s<<c->getSkew();
		DOMText* t_sk=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_sk->appendChild(t_sk);
		_clock->appendChild(_sk);
	}
	if(c->isDerived())
	{
		
		DOMElement* _dfi=wDoc->createElement(tags.derived_from_id);
		s.str("");
		s.clear();
		s<<c->getDerivedID();
		DOMText* t_dfi=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_dfi->appendChild(t_dfi);
		_clock->appendChild(_dfi);

		DOMElement* _dt=wDoc->createElement(tags.derivation_type);
		s.str("");
		s.clear();
		s<<c->getDerivType();
		DOMText* t_dt=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_dt->appendChild(t_dt);
		_clock->appendChild(_dt);

		DOMElement* _dp=wDoc->createElement(tags.derivation_phase);
		s.str("");
		s.clear();
		s<<c->getDerivPhase();
		DOMText* t_dp=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_dp->appendChild(t_dp);
		_clock->appendChild(_dp);
	}
	return _clock;
}

DOMElement* cef_parser::convertPowerNet(cef_powernet* p)
{
	vector<double>* voltage;
	
	stringstream s;
	DOMElement* _pnet=wDoc->createElement(tags.clock);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(p->getName().c_str()));
	_name->appendChild(t_name);
	_pnet->appendChild(_name);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<p->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_pnet->appendChild(_id);
	
	DOMElement* _v;
	DOMText* t_v;
	voltage=p->getVoltage();
	for(unsigned i=0;i<voltage->size();i++)
	{
		_v=wDoc->createElement(tags.voltage);
		s.str("");
		s.clear();
		s<<(*voltage)[i];
		t_v=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_v->appendChild(t_v);
		_pnet->appendChild(_v);
	}
	if(p->isDerived())
	{
		
		DOMElement* _dfi=wDoc->createElement(tags.derived_from_id);
		s.str("");
		s.clear();
		s<<p->getDerivedID();
		DOMText* t_dfi=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_dfi->appendChild(t_dfi);
		_pnet->appendChild(_dfi);
	}
	return _pnet;
}

DOMElement* cef_parser::convertDomain(cef_domain* dom)
{
	DOMElement* _domain=wDoc->createElement(tags.domain);	
	
	stringstream s;
	DOMElement* _pnet=wDoc->createElement(tags.clock);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(dom->getName().c_str()));
	_name->appendChild(t_name);
	_domain->appendChild(_name);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<dom->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_domain->appendChild(_id);

	DOMElement* _cid=wDoc->createElement(tags.clock_id);
	s.str("");
	s.clear();
	s<<dom->getClockID();
	DOMText* t_cid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_cid->appendChild(t_cid);
	_domain->appendChild(_cid);

	DOMElement* _pid=wDoc->createElement(tags.power_network_id);
	s.str("");
	s.clear();
	s<<dom->getPowerID();
	DOMText* t_pid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_pid->appendChild(t_pid);
	_domain->appendChild(_pid);

	DOMElement* _oppts=wDoc->createElement(tags.operating_points);
	DOMElement* _op;
	vector<cef_operating_point*>* ops=dom->getOpPts();
	if(dom->getOpPts()!=NULL && dom->getOpPts()->size()>0)
	{
		for(unsigned i=0;i<ops->size();i++)
		{
			_op=convertOperatingPoint((*ops)[i]);	
			_oppts->appendChild(_op);
		}
		_domain->appendChild(_oppts);
	}
	return _domain;
		
	
}

DOMElement* cef_parser::convertOperatingPoint(cef_operating_point* op)
{
	stringstream s;
	DOMElement* _op=wDoc->createElement(tags.operating_point);	

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<op->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_op->appendChild(_id);

	DOMElement* _clock=wDoc->createElement(tags.clock);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(op->getName().c_str()));
	_name->appendChild(t_name);
	_op->appendChild(_name);


	DOMElement* _f=wDoc->createElement(tags.frequency);
	s.str("");
	s.clear();
	s<<op->getFrequency();
	DOMText* t_f=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_f->appendChild(t_f);
	_op->appendChild(_f);
	
	DOMElement* _v=wDoc->createElement(tags.voltage);
	s.str("");
	s.clear();
	s<<op->getVoltage();
	DOMText* t_v=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_f->appendChild(t_v);
	_op->appendChild(_v);
	
	return _op;

}


DOMElement* cef_parser::convertLink(cef_link* link)
{
	
	stringstream s;
	string mybool;

	DOMElement* _link=wDoc->createElement(tags.link);

	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(link->getName().c_str()));
	_name->appendChild(t_name);
	_link->appendChild(_name);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<link->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_link->appendChild(_id);
	
	DOMElement* _srcid=wDoc->createElement(tags.source_port_id);
	s.str("");
	s.clear();
	s<<link->getSrc();
	DOMText* t_srcid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_srcid->appendChild(t_srcid);
	_link->appendChild(_srcid);

	DOMElement* _destid=wDoc->createElement(tags.destination_port_id);
	s.str("");
	s.clear();
	s<<link->getDst();
	DOMText* t_destid=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_destid->appendChild(t_destid);
	_link->appendChild(_destid);
	
	if(link->getSrcClk()) mybool="True";
	else mybool="False";

	DOMElement* _srcclk=wDoc->createElement(tags.carries_source_clock);
	DOMText* t_srcclk=wDoc->createTextNode(sm->convert(mybool.c_str()));
	_srcclk->appendChild(t_srcclk);
	_link->appendChild(_srcclk);

	if(link->getSrcRst()) mybool="True";
	else mybool="False";

	DOMElement* _srcrst=wDoc->createElement(tags.carries_source_reset);
	DOMText* t_srcrst=wDoc->createTextNode(sm->convert(mybool.c_str()));
	_srcrst->appendChild(t_srcrst);
	_link->appendChild(_srcrst);
	
	if(link->getAuxF()>0)	
	{
		
		DOMElement* _auxf=wDoc->createElement(tags.auxilary_forward_wires);
		s.str("");
		s.clear();
		s<<link->getAuxF();
		DOMText* t_auxf=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_auxf->appendChild(t_auxf);
		_link->appendChild(_auxf);
	}

	if(link->getAuxB()>0)	
	{
		
		DOMElement* _auxb=wDoc->createElement(tags.auxilary_backward_wires);
		s.str("");
		s.clear();
		s<<link->getAuxB();
		DOMText* t_auxb=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_auxb->appendChild(t_auxb);
		_link->appendChild(_auxb);
	}
	
	if(link->getBlocks()!=NULL && link->getBlocks()->size()>0)	
	{	
		DOMElement* _blocks=wDoc->createElement(tags.blocks_on_link);
		DOMElement* _block;
		DOMText* t_block;
		vector<unsigned>* bloks=link->getBlocks();		
		for(unsigned i=0;i<bloks->size();i++)
		{
			s.str("");
			s.clear();
			s<<(*bloks)[i];
			_block=wDoc->createElement(tags.block_on_link);	
			t_block=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_block->appendChild(t_block);
			_blocks->appendChild(_block);
		}
		_link->appendChild(_blocks);
	}
	return _link;
}

DOMElement* cef_parser::convertBlock(cef_block* block)
{
	stringstream s;
	DOMElement* _block=wDoc->createElement(tags.block);
	DOMElement* _name=wDoc->createElement(tags.name);
	DOMText* t_name=wDoc->createTextNode(sm->convert(block->getName().c_str()));
	_name->appendChild(t_name);
	_block->appendChild(_name);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<block->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_block->appendChild(_id);


	DOMElement* _block_type=wDoc->createElement(tags.block_type);
	s.str("");
	s.clear();
	s<<block->getType();
	DOMText* t_block_type=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_block_type->appendChild(t_block_type);
	_block->appendChild(_block_type);

	if(block->getConnections()!=NULL && block->getConnections()->size()>0)
	{
		vector<unsigned>* cons=block->getConnections();
		DOMElement* _connections=wDoc->createElement(tags.connections);
		DOMElement* _connection;
		DOMText* t_connection;
		for(unsigned i=0;i<cons->size();i++)		
		{
			_connection=wDoc->createElement(tags.connection_with_id);
			s.str("");
			s.clear();
			s<<(*cons)[i];
			t_connection=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_connection->appendChild(t_connection);
			_connections->appendChild(_connection);
		}
		_block->appendChild(_connections);
	}
	DOMElement *_ports=wDoc->createElement(tags.ports);
	DOMElement *_port;
	vector<cef_port*>* myports=block->getPorts();
	for(unsigned i=0;i<myports->size();i++)
	{
		_port=convertPort((*myports)[i]);	
		_ports->appendChild(_port);
	}
	_block->appendChild(_ports);
	if(block->getSize()!=NULL)
	{
		DOMElement* _size=convertBlockSize(block->getSize());	
		_block->appendChild(_size);
	}
	if(block->getBotLefCor()!=NULL)
	{
		DOMElement* _blc=convertBlockPosition(block->getBotLefCor());	
		_block->appendChild(_blc);
	}
	if(block->getOrientation())
	{
		DOMElement* _ori=convertBlockOrientation(block->getOrientation());
		_block->appendChild(_ori);	
	}
		
	DOMElement* _layer=wDoc->createElement(tags.layer);
	s.str("");
	s.clear();
	s<<block->getLayer();
	DOMText* t_layer=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_layer->appendChild(t_layer);
	_block->appendChild(_layer);

	if(block->getArchitecture())
	{
		DOMElement* _arc=convertBlockArchitecture(block->getArchitecture());
		_block->appendChild(_arc);
	}
	return _block;
	
}

DOMElement* cef_parser::convertPort(cef_port* port)
{
	
	stringstream s;

	DOMElement* _port=wDoc->createElement(tags.port);

	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<port->getID();
	DOMText* t_id=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t_id);
	_port->appendChild(_id);

	DOMElement* _protocol=wDoc->createElement(tags.protocol);
	DOMText* t_protocol=wDoc->createTextNode(sm->convert(port->getProtocol().c_str()));
	_protocol->appendChild(t_protocol);
	_port->appendChild(_protocol);
	if(port->getProtocolVar().size()>0)
	{
		DOMElement* _protocolvar=wDoc->createElement(tags.protocol_variant);
		DOMText* t_protocolvar=wDoc->createTextNode(sm->convert(port->getProtocolVar().c_str()));
		_protocolvar->appendChild(t_protocolvar);
		_port->appendChild(_protocolvar);
	}
	if(port->getMOT()>0)
	{
		DOMElement* _mot=wDoc->createElement(tags.max_outstanding_transactions);
		s.str("");
		s.clear();
		s<<port->getMOT();
		DOMText* t_mot=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_mot->appendChild(t_mot);
		_port->appendChild(_mot);
	}
	if(port->getAddressWidth()>0)
	{
		DOMElement* _addw=wDoc->createElement(tags.address_width);
		s.str("");
		s.clear();
		s<<port->getAddressWidth();
		DOMText* t_addw=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_addw->appendChild(t_addw);
		_port->appendChild(_addw);
	}
	if(port->getReadDataWidth()>0)
	{
		DOMElement* _rdw=wDoc->createElement(tags.read_data_width);
		s.str("");
		s.clear();
		s<<port->getReadDataWidth();
		DOMText* t_rdw=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_rdw->appendChild(t_rdw);
		_port->appendChild(_rdw);
	}
	if(port->getWriteDataWidth()>0)
	{
		DOMElement* _wdw=wDoc->createElement(tags.write_data_width);
		s.str("");
		s.clear();
		s<<port->getWriteDataWidth();
		DOMText* t_wdw=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_wdw->appendChild(t_wdw);
		_port->appendChild(_wdw);
	}
	if(port->getFlitWidth()>0)
	{
		DOMElement* _fw=wDoc->createElement(tags.flit_width);
		s.str("");
		s.clear();
		s<<port->getFlitWidth();
		DOMText* t_fw=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_fw->appendChild(t_fw);
		_port->appendChild(_fw);
	}
	if(port->getPosition())
	{
		DOMElement* _posx=wDoc->createElement(tags.position_x);
		s.str("");
		s.clear();
		s<<port->getPosition()->first;
		DOMText* t_posx=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_posx->appendChild(t_posx);
		_port->appendChild(_posx);

		DOMElement* _posy=wDoc->createElement(tags.position_y);
		s.str("");
		s.clear();
		s<<port->getPosition()->second;
		DOMText* t_posy=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_posy->appendChild(t_posy);
		_port->appendChild(_posy);
	}
	DOMElement* _did=wDoc->createElement(tags.domain_id);
	s.str("");
	s.clear();
	s<<port->getDomainID();
	DOMText* t_did=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_did->appendChild(t_did);
	_port->appendChild(_did);
	return _port;
}

DOMElement* cef_parser::convertBlockSize(cef_block_size* size)
{
	DOMElement* _size=wDoc->createElement(tags.size);
	DOMElement* _soft=wDoc->createElement(tags.soft);
	stringstream s;
	DOMText* t_soft;
	if(size->getSoft())
		t_soft=wDoc->createTextNode(sm->convert("True"));
	else
		t_soft=wDoc->createTextNode(sm->convert("False"));
	_soft->appendChild(t_soft);
	_size->appendChild(_soft);
	if(!size->getSoft())	
	{
		DOMElement* _shape=wDoc->createElement(tags.shape);	
		DOMElement* _corners=wDoc->createElement(tags.corners);
		DOMElement* _corner,*_x,*_y;
		DOMText* t_x,*t_y;
		vector<pair<double,double>*>* corners=size->getShape();
		for(unsigned i=0;i<corners->size();i++)
		{
			_corner=wDoc->createElement(tags.corner);	
			_x=wDoc->createElement(tags.x);
			s.str("");
			s.clear();
			s<<(*corners)[i]->first;
			t_x=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_x->appendChild(t_x);	
			_y=wDoc->createElement(tags.y);
			s.str("");
			s.clear();
			s<<(*corners)[i]->second;
			t_y=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_corner->appendChild(_x);
			_corner->appendChild(_y);
			_corners->appendChild(_corner);
		}
		_shape->appendChild(_corners);
		_size->appendChild(_shape);
	}
	if(size->getArea()>0)	
	{
	
		DOMElement* _area=wDoc->createElement(tags.area);
		s.str("");
		s.clear();
		s<<size->getArea();
		DOMText* t_area=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_area->appendChild(t_area);
		_size->appendChild(_area);
	}
	if(size->getAspectRatio()>0)	
	{
	
		DOMElement* _ar=wDoc->createElement(tags.aspectratio);
		s.str("");
		s.clear();
		s<<size->getAspectRatio();
		DOMText* t_ar=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_ar->appendChild(t_ar);
		_size->appendChild(_ar);
	}
	return _size;
}


DOMElement* cef_parser::convertBlockPosition(cef_block_blc_pos* pos)
{
	stringstream s;
	DOMElement* _pos=wDoc->createElement(tags.bottom_left_corner_position);
	DOMElement* _x=wDoc->createElement(tags.x);
	s.str("");
	s.clear();
	s<<pos->getX();
	DOMText* t_x=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_x->appendChild(t_x);
	_pos->appendChild(_x);
	
	DOMElement* _y=wDoc->createElement(tags.y);
	s.str("");
	s.clear();
	s<<pos->getY();
	DOMText* t_y=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_y->appendChild(t_y);
	_pos->appendChild(_y);

	DOMElement* _mov=wDoc->createElement(tags.movable);
	DOMText* t_mov;
	if(pos->getMove())
		t_mov=wDoc->createTextNode(sm->convert("True"));
	else
		t_mov=wDoc->createTextNode(sm->convert("False"));
	_mov->appendChild(t_mov);
	_pos->appendChild(_mov);

	DOMElement* _ix=wDoc->createElement(tags.initial_x);
	s.str("");
	s.clear();
	s<<pos->getInitX();
	DOMText* t_ix=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_ix->appendChild(t_ix);
	_pos->appendChild(_ix);
	
	DOMElement* _iy=wDoc->createElement(tags.initial_y);
	s.str("");
	s.clear();
	s<<pos->getInitY();
	DOMText* t_iy=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_iy->appendChild(t_iy);
	_pos->appendChild(_iy);

	return _pos;
}

DOMElement* cef_parser::convertBlockOrientation(cef_block_orientation* ori)
{
	stringstream s;
	DOMElement* _ori=wDoc->createElement(tags.orientation);	
	
	DOMElement* _rot=wDoc->createElement(tags.rotatable);
	DOMText* t_rot;
	if(ori->getRotate())
		t_rot=wDoc->createTextNode(sm->convert("True"));
	else
		t_rot=wDoc->createTextNode(sm->convert("False"));
	_rot->appendChild(t_rot);
	_ori->appendChild(_rot);

	DOMElement* _mir=wDoc->createElement(tags.mirrorable);
	DOMText* t_mir;
	if(ori->getMirror())
		t_mir=wDoc->createTextNode(sm->convert("True"));
	else
		t_mir=wDoc->createTextNode(sm->convert("False"));
	_mir->appendChild(t_mir);
	_ori->appendChild(_mir);

	DOMElement* _rt=wDoc->createElement(tags.rotation);
	s.str("");
	s.clear();
	s<<(unsigned)ori->getRotation();
	DOMText* t_rt=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_rt->appendChild(t_rt);
	_ori->appendChild(_rt);

	DOMElement* _irt=wDoc->createElement(tags.initial_rotation);
	s.str("");
	s.clear();
	s<<(unsigned)ori->getInitRotation();
	DOMText* t_irt=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_irt->appendChild(t_irt);
	_ori->appendChild(_irt);

	DOMElement* _mx=wDoc->createElement(tags.mirroring_x);
	DOMText* t_mx;
	if(ori->getMirroringX())
		t_mx=wDoc->createTextNode(sm->convert("True"));
	else
		t_mx=wDoc->createTextNode(sm->convert("False"));
	_mx->appendChild(t_mx);
	_ori->appendChild(_mx);

	DOMElement* _my=wDoc->createElement(tags.mirroring_y);
	DOMText* t_my;
	if(ori->getMirroringY())
		t_my=wDoc->createTextNode(sm->convert("True"));
	else
		t_my=wDoc->createTextNode(sm->convert("False"));
	_my->appendChild(t_my);
	_ori->appendChild(_my);

	DOMElement* _imx=wDoc->createElement(tags.initial_mirroring_x);
	DOMText* t_imx;
	if(ori->getMirroringX())
		t_imx=wDoc->createTextNode(sm->convert("True"));
	else
		t_imx=wDoc->createTextNode(sm->convert("False"));
	_imx->appendChild(t_imx);
	_ori->appendChild(_imx);

	DOMElement* _imy=wDoc->createElement(tags.initial_mirroring_y);
	DOMText* t_imy;
	if(ori->getMirroringY())
		t_imy=wDoc->createTextNode(sm->convert("True"));
	else
		t_imy=wDoc->createTextNode(sm->convert("False"));
	_imy->appendChild(t_imy);
	_ori->appendChild(_imy);
	return _ori;
}

DOMElement* cef_parser::convertBlockArchitecture(cef_block_architecture* arc)
{
	stringstream s;
	DOMElement* _arc=wDoc->createElement(tags.architecture);
	if(arc->getNIType().size()>0)
	{
		DOMElement* _nic=wDoc->createElement(tags.ni_type);
		DOMText* t_nic=wDoc->createTextNode(sm->convert(arc->getNIType().c_str()));
		_nic->appendChild(t_nic);
		_arc->appendChild(_nic);
	}
	if(arc->getAddrRngs()!=NULL && arc->getAddrRngs()->size()>0)
	{
		vector<pair<unsigned long long,unsigned long long>*>* ranges=arc->getAddrRngs();
		DOMElement* _ads=wDoc->createElement(tags.address_ranges);
		DOMElement* _ad,*_st,*_sz;
		DOMText* t;
		for(unsigned i=0;i<ranges->size();i++)
		{
			_ad=wDoc->createElement(tags.address_range);			
			_st=wDoc->createElement(tags.start);
			_sz=wDoc->createElement(tags.size);
			s.str("");
			s.clear();
			s<<(*ranges)[i]->first;
			t=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_st->appendChild(t);
			s<<(*ranges)[i]->second;
			t=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_sz->appendChild(t);
			_ad->appendChild(_st);
			_ad->appendChild(_sz);
			_ads->appendChild(_ad);	
		}
		_arc->appendChild(_ads);
	}
	DOMElement* _dep;
	DOMText* t_dep;
	if(arc->getInBuffers()!=NULL && arc->getInBuffers()->size()>0)
	{
		vector<unsigned>* inb=arc->getInBuffers();
		DOMElement* _inb=wDoc->createElement(tags.input_buffers);
		for(unsigned i=0;i<inb->size();i++)
		{
			_dep=wDoc->createElement(tags.depth);				
			s.str("");
			s.clear();
			s<<(*inb)[i];
			t_dep=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_dep->appendChild(t_dep);	
			_inb->appendChild(_dep);
		}
		_arc->appendChild(_inb);
	}
	if(arc->getOutBuffers()!=NULL && arc->getOutBuffers()->size()>0)
	{
		vector<unsigned>* outb=arc->getOutBuffers();
		DOMElement* _outb=wDoc->createElement(tags.output_buffers);
		for(unsigned i=0;i<outb->size();i++)
		{
			_dep=wDoc->createElement(tags.depth);				
			s.str("");
			s.clear();
			s<<(*outb)[i];
			t_dep=wDoc->createTextNode(sm->convert(s.str().c_str()));
			_dep->appendChild(t_dep);	
			_outb->appendChild(_dep);
		}
		_arc->appendChild(_outb);
	}
	if(arc->getVCs()>0)
	{
		DOMElement* _vcs=wDoc->createElement(tags.virtual_channels);
		s.str("");
		s.clear();
		s<<arc->getVCs();
		DOMText* t_vcs=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_vcs->appendChild(t_vcs);
		_arc->appendChild(_vcs);
	}
	if(arc->getVCArbStruct().size()>0)  
	{
	
		DOMElement* _vcas=wDoc->createElement(tags.virtual_channel_arbitration_structure);
		DOMText* t_vcas=wDoc->createTextNode(sm->convert(arc->getVCArbStruct().c_str()));
		_vcas->appendChild(t_vcas);
		_arc->appendChild(_vcas);
	}
	if(arc->getArbit().size()>0)  
	{
	
		DOMElement* _arbit=wDoc->createElement(tags.arbitration_policy);
		DOMText* t_arbit=wDoc->createTextNode(sm->convert(arc->getArbit().c_str()));
		_arbit->appendChild(t_arbit);
		_arc->appendChild(_arbit);
	}
	if(arc->getVCArbit().size()>0)  
	{
	
		DOMElement* _vcarbit=wDoc->createElement(tags.virtual_channel_arbitration_policy);
		DOMText* t_vcarbit=wDoc->createTextNode(sm->convert(arc->getVCArbit().c_str()));
		_vcarbit->appendChild(t_vcarbit);
		_arc->appendChild(_vcarbit);
	}
	return _arc;
}
DOMElement* cef_parser::convertRoutes(cef_routes* myroutes)
{
	stringstream s;
	DOMElement* _routes=wDoc->createElement(tags.routes);
	DOMElement* _route;
	DOMElement* _routingpolicy=wDoc->createElement(tags.routingpolicy);
	s.str("");
	s.clear();
	s<<myroutes->getRoutingPolicy();
	DOMText* t_rp=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_routingpolicy->appendChild(t_rp);
	_routes->appendChild(_routingpolicy);
	if(myroutes->getSourceRouting())	
	{
		_route=convertSourceRouting(myroutes->getSourceRouting());
		_routes->appendChild(_route);
	}
	if(myroutes->getDistributedRouting())	
	{
		_route=convertDistributedRouting(myroutes->getDistributedRouting());
		_routes->appendChild(_route);
	}
	if(myroutes->getLBDR())	
	{
		_route=convertLBDR(myroutes->getLBDR());
		_routes->appendChild(_route);
	}
	if(myroutes->getFDOR())	
	{
		_route=convertLBDR(myroutes->getLBDR());
		_routes->appendChild(_route);
	}
	if(myroutes->getVNs())	
	{
		_route=convertVirtualNets(myroutes->getVNs());
		_routes->appendChild(_route);
	}
	return _routes;
}

DOMElement* cef_parser::convertSourceRouting(cef_sourcerouting* src)
{
	DOMElement* _src=wDoc->createElement(tags.source_routing);
	vector<cef_src_route*>* routz=src->getRoutes();	
	DOMElement* _route;
	for(unsigned i=0;i<routz->size();i++)
	{
		_route=convertSrcRoute((*routz)[i]);	
		_src->appendChild(_route);	
	}
	return _src;
}

DOMElement* cef_parser::convertSrcRoute(cef_src_route* route)
{
	stringstream s;
	DOMElement* _route=wDoc->createElement(tags.route);
	DOMText* t;
	DOMElement* _srcid=wDoc->createElement(tags.source_id);
	s<<route->getSrcID();	
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_srcid->appendChild(t);	
	_route->appendChild(_srcid);
	DOMElement* _dstid=wDoc->createElement(tags.destination_id);
	s.str("");
	s.clear();
	s<<route->getDstID();	
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_dstid->appendChild(t);	
	_route->appendChild(_dstid);
	DOMElement* _swpids=wDoc->createElement(tags.switch_output_port_ids);
	
	DOMElement* _swpid;
	vector<unsigned>* sopi=route->getSOPI();
	for(unsigned i=0;i<sopi->size();i++)
	{
		s.str("");
		s.clear();
		s<<(*sopi)[i];
		_swpid=wDoc->createElement(tags.switch_output_port_id);
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_swpid->appendChild(t);
		_swpids->appendChild(_swpid);
	}
	_route->appendChild(_swpids);
	if(route->getWeight()>=0)
	{
		s.str("");
		s.clear();
		s<<route->getWeight();
		DOMElement* _w=wDoc->createElement(tags.weight);
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_w->appendChild(t);
		_route->appendChild(_w);
	}
	return _route;
}


DOMElement* cef_parser::convertDistributedRouting(cef_distributedrouting* route)
{
	DOMElement* _route=wDoc->createElement(tags.distributed_routing);
	DOMElement* _tables=wDoc->createElement(tags.tables);
	vector<cef_routing_table*>* tables=route->getTables();
	DOMElement* _table;
	for(unsigned i=0;i<tables->size();i++)
	{
		_table=convertTable((*tables)[i]);	
		_tables->appendChild(_table);
	}
	_route->appendChild(_tables);
	return _route;
}

DOMElement* cef_parser::convertTable(cef_routing_table* table)
{
	stringstream s;	
	DOMElement* _table=wDoc->createElement(tags.table);
	DOMText* t;
	DOMElement* _swid=wDoc->createElement(tags.switch_id);
	s.str("");
	s.clear();
	s<<table->getSwID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_swid->appendChild(t);
	_table->appendChild(_swid);
	DOMElement* _dstid=wDoc->createElement(tags.destination_id);
	s.str("");
	s.clear();
	s<<table->getDstID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_dstid->appendChild(t);
	_table->appendChild(_dstid);
	DOMElement* _inpid=wDoc->createElement(tags.input_port_id);
	s.str("");
	s.clear();
	s<<table->getInpID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_inpid->appendChild(t);
	_table->appendChild(_inpid);
	DOMElement* _outports=wDoc->createElement(tags.output_ports);
	vector<pair<unsigned,double>*>* oports=table->getOutPorts();
	DOMElement* _outport;
	DOMElement* _outportid;
	DOMElement* _weight;
	for(unsigned i=0;i<oports->size();i++)
	{
		s.str("");
		s.clear();
		s<<(*oports)[i]->first;
		_outport=wDoc->createElement(tags.output_port);
		_outportid=wDoc->createElement(tags.output_port_id);
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_outportid->appendChild(t);
		_outport->appendChild(_outportid);
		s.str("");
		s.clear();
		s<<(*oports)[i]->second;
		_weight=wDoc->createElement(tags.route_weight);
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_weight->appendChild(t);
		_outport->appendChild(_weight);
		_outports->appendChild(_outport);
	}
	_table->appendChild(_outports);
	return _table;
}

DOMElement* cef_parser::convertLBDR(cef_lbdr* lbdr)
{
	DOMElement* _lbdr=wDoc->createElement(tags.lbdr);
	DOMElement* _lbdre;
	vector<cef_lbdr_entry*>* entries=lbdr->getEntries();
	for(unsigned i=0;i<entries->size();i++)
	{
		_lbdre=convertLBDREntry((*entries)[i]);
		_lbdr->appendChild(_lbdre);
	}
	return _lbdr;
}


DOMElement* cef_parser::convertLBDREntry(cef_lbdr_entry* entry)
{
	stringstream s;
	DOMText* t;
	DOMElement* _entry=wDoc->createElement(tags.lbdr_entry);
	DOMElement* _swid=wDoc->createElement(tags.switch_id);
	s.str("");
	s.clear();
	s<<entry->getSwID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));	
	_swid->appendChild(t);
	_entry->appendChild(_swid);
	DOMElement* _rb=wDoc->createElement(tags.lbdr_routing_bits);
	s.str("");
	s.clear();
	s<<u2str(entry->getRoutingBits());
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));	
	_rb->appendChild(t);
	_entry->appendChild(_rb);
	DOMElement* _cb=wDoc->createElement(tags.lbdr_connectivity_bits);
	s.str("");
	s.clear();
	s<<u2str(entry->getConnectBits());
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));	
	_cb->appendChild(t);
	_entry->appendChild(_cb);
	DOMElement* _deroutes=wDoc->createElement(tags.lbdr_deroutes);
	DOMElement* _deroute,*_inpid,*_dport;
	vector<pair<unsigned,unsigned>*>* deroutes=entry->getDeroutes();
	for(unsigned i=0;i<deroutes->size();i++)
	{
		_deroute=wDoc->createElement(tags.lbdr_deroute);	
		_inpid=wDoc->createElement(tags.input_port_id);
		s.str("");
		s.clear();
		s<<(*deroutes)[i]->first;
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));	
		_inpid->appendChild(t);
		_deroute->appendChild(_inpid);
		
		_dport=wDoc->createElement(tags.deroute_port);
		s.str("");
		s.clear();
		s<<(*deroutes)[i]->second;
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));	
		_dport->appendChild(t);
		_deroute->appendChild(_dport);
		_deroutes->appendChild(_deroute);
	}
	_entry->appendChild(_deroutes);
	DOMElement* _forks=wDoc->createElement(tags.lbdr_forks);
	DOMElement* _fork;
	vector<cef_lbdr_fork*>* forks=entry->getForks();
	for(unsigned i=0;i<forks->size();i++)
	{
		_fork=convertLBDRFork((*forks)[i]);	
		_forks->appendChild(_fork);
	}
	_entry->appendChild(_forks);
	return _entry;
}

DOMElement* cef_parser::convertLBDRFork(cef_lbdr_fork* fork)
{
	stringstream s;	
	DOMText* t;
	DOMElement* _fork=wDoc->createElement(tags.lbdr_fork);
	DOMElement* _inpid=wDoc->createElement(tags.input_port_id);
	s.str("");
	s.clear();
	s<<fork->getInpID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_inpid->appendChild(t);
	_fork->appendChild(_inpid);
	DOMElement* _fps=wDoc->createElement(tags.fork_ports);
	DOMElement* _fp;
	vector<unsigned>* fps=fork->getForkPorts();
	for(unsigned i=0;i<fps->size();i++)
	{
		s.str("");
		s.clear();
		s<<(*fps)[i];
		_fp=wDoc->createElement(tags.fork_port);		
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_fp->appendChild(t);
		_fps->appendChild(_fp);
	}
	_fork->appendChild(_fps);
	return _fork;
}

DOMElement* cef_parser::convertFDOR(cef_fdor* fdor)
{
	stringstream s;
	DOMElement* _fdor=wDoc->createElement(tags.fdor);
	DOMElement* _entry;
	DOMElement* _swid,*_bits;
	DOMText* t;
	vector<pair<unsigned,unsigned long long>*>* entries=fdor->getEntries();
	for(unsigned i=0;i<entries->size();i++)
	{
		_entry=wDoc->createElement(tags.fdor_entry);	
		_swid=wDoc->createElement(tags.switch_id);
		s.str("");
		s.clear();
		s<<(*entries)[i]->first;
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_swid->appendChild(t);
		_entry->appendChild(_swid);
		_bits=wDoc->createElement(tags.fdor_bits);
		s.str("");
		s.clear();
		s<<(*entries)[i]->second;
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_bits->appendChild(t);
		_entry->appendChild(_bits);
		_fdor->appendChild(_entry);
	}
	return _fdor;
}
DOMElement* cef_parser::convertVirtualNets(vector<cef_virtual_network*>* vns)
{
	DOMElement* _vns=wDoc->createElement(tags.virtual_networks);
	DOMElement* _vn;
	for(unsigned i=0;i<vns->size();i++)	
	{
		_vn=convertVirtualNet((*vns)[i]);
		_vns->appendChild(_vn);
	}
	return _vns;
}

DOMElement* cef_parser::convertVirtualNet(cef_virtual_network* vn)
{
	stringstream s;
	DOMText* t;
	DOMElement* _vn=wDoc->createElement(tags.virtual_network);
	DOMElement* _id=wDoc->createElement(tags.id);
	s.str("");
	s.clear();
	s<<vn->getID();
	t=wDoc->createTextNode(sm->convert(s.str().c_str()));
	_id->appendChild(t);
	_vn->appendChild(_id);
	DOMElement* _vcids=wDoc->createElement(tags.vc_ids);
	DOMElement* _vcid;
	vector<unsigned>* vcids=vn->getVCs();
	for(unsigned i=0;i<vcids->size();i++)
	{
		s.str("");
		s.clear();	
		s<<(*vcids)[i];
		_vcid=wDoc->createElement(tags.vc_id);
		t=wDoc->createTextNode(sm->convert(s.str().c_str()));
		_vcid->appendChild(t);
		_vcids->appendChild(_vcid);
	}
	_vn->appendChild(_vcids);
	return _vn;
}
cef_parser::cef_parser()
{
	sm=new StringManager();	
	
}
cef_parser::~cef_parser()
{
	delete sm;

}

cef* cef_parser::parseXML(std::string xmlfile)
{
	myparser.setValidationScheme( xercesc::XercesDOMParser::Val_Never ) ;
	myparser.setDoNamespaces( false ) ;
	myparser.setDoSchema( false ) ;
	myparser.setLoadExternalDTD( false ) ;
	myparser.parse( xmlfile.c_str() ) ;
	DOMDocument* xmlDoc = myparser.getDocument();
	DOMElement* elementCEF= xmlDoc->getDocumentElement();
	handleCEF(elementCEF);
	return mycef;
}
